node.js - Simple Auth middleware with bcrypt is not functionning on postman -
i quite new in backend, , first time using middleware nodejs. until now, register function , format password functionning on postman, since when register new user, find on db hashed password.
however, when want log in same user, receive message (wrong password or mail). console.log(user) read user profil on node, after, seems bcrypt.comparesync not functionning. id console.log(token), returns nothing.
i surely have problem on generatetoken function or on login function cannot figure out.
you can see below code.
login (req,res){ users.find({mail:req.body.mail}) .then(users =>{ console.log(users); if(users.length > 0 && bcrypt.comparesync(req.body.mail+req.body.password, users[0].password)){ const token= generatetoken(users[0]); console.log(token); res.status(200).send('operation succeed: \n' + token); //res.statut(200).redirect('/logged'); } else{ res.status(500).send('wrong password or mail'); } }) .catch(err =>{ res.send(err); }); },
}
my generatetoken function:
function generatetoken(user){ const payload={ iat:moment().unix(), exp:moment().add(14,'days').unix(), iss:user.mail, sub:user.password } return jsonwebtoken.sign(payload,'app_secret');
}
thank help.
Comments
Post a Comment