postman Node.js API用户请求检查函数

wljmcqd8  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(164)

我的日志API有一些问题很难找到。注册API工作正常,并在检查后,显示我的用户名,密码和一切,但当我试图检查日志,显示一个空参数。我的日志功能有什么问题?我必须安装CryptoJS加密密码。请检查他们。

const router = require("express").Router();
const User = require("../models/User");
const CryptoJS = require("crypto-js");

//Register
router.post("/register", async (req, res) => {
    const newUser = new User({
        username: req.body.username,
        email: req.body.email,
        password: CryptoJS.AES.encrypt(
            req.body.password, 
            process.env.PASS_SEC
            ).toString(),
    });
    try{
        const savedUser = await newUser.save();
        res.status(201).json(savedUser);
       }catch(err){
        res.status(500).json(err);
    }
});

//Login
router.post('/login', async (req, res) => {
    try{
        const user = await User.findOne({ username: req.body.username });
        !user && res.status(401).json("Wrong credentials!");

        const hashedPassword = CryptoJS.AES.decrypt(
            user.password,
            process.env.PASS_SEC
        );
        const password = hashedPassword.toString(CryptoJS.enc.Utf8);

        password !== req.body.password && 
            res.status(401).json("Wrong credentials!");

            res.status(500).json(err);
    }catch (err){
        res.status(500).json(err);
    }
});

module.exports = router;

euoag5mw

euoag5mw1#

上一个资源状态导致了问题

router.post('/login', async (req, res) => {
    try{
        const user = await User.findOne({ username: req.body.username });
        !user && res.status(401).json("Wrong credentials!");

        const hashedPassword = CryptoJS.AES.decrypt(
            user.password,
            process.env.PASS_SEC
        );
        const password = hashedPassword.toString(CryptoJS.enc.Utf8);

        password !== req.body.password && 
            res.status(401).json("Wrong credentials!");

            res.status(200).json(user); //This means all the cases are succesfull so you should pass the user(with necessary properties)
    }catch (err){
        res.status(500).json(err);
    }
});

相关问题