Using jsonwebtoken always prompts that the parameters are wrong, and if you bring up the parameters and declare them with a single variable, you will not report an error. What is the reason for this?

// keys.js
module.exports = {
    mongoURI: "",
    secretOrKey: "secret"
}
const keys = require("../../config/keys");

router.post("/login", (req, res) => {
    const email = req.body.email;
    const password = req.body.password;
    
    User.findOne({ email }).then(user => {
        if (!user) {
            return res.status(404).json({ email: "!" })
        }

        // Load hash from your password DB.
        bcrypt.compare(password, user.password, (err, isMatch)=>{
            if(isMatch){
                const rule = {id:user.id,name:user.name};
                jwt.sign({rule, keys.secretOrKey, {expiresIn:3600}, (err,token)=>{
                    if(err)throw err;
                    res.json({
                        success:true,
                        token:"partiny"+token
                    });
                }});                
                res.json({msg:"success"});
            }else{
                return res.status(400).json({password:""});
            }
        });
    })
})

clipboard.png

Apr.15,2022

jwt.sign(rule, keys.secretOrKey, {expiresIn:3600}, (err,token)=>{
    if(err)throw err;
    res.json({
        success:true,
        token:"partiny"+token
    });
});

write two more parentheses:
clipboard.png

rule is preceded by a parenthesis, which means you pass an object to the sign function:

let a = {rule} //  let a = {rule: rule}
// 
let a = {rule, keys.secretOrKey} // let a = {rule: rule, keys.secretOrKey: keys.secretOrKey}?
Menu