userCtrl 3/3
Tue Sep 07 2021 22:01:33 GMT+0000 (Coordinated Universal Time)
Saved by
@devdave
exports.login = (req, res, next) => {
User.findOne({ email: req.body.email })
.then((user) => {
if (!user) {
return res.status(401).json({ error: "User non trouvé !" });
}
bcrypt
.compare(req.body.password, user.password)
.then((valid) => {
if (!valid) {
return res.status(401).json({ error: "Password incorrect !" });
}
res.status(200).json({
userId: user._id,
// jsonWebToken => function "sign", takes 3 args
// token will hold encoded userId => create new signed objects and auth
// if user[1] uploads a new sauce, it prevents user[2] to modify it
token: jwt.sign(
{
// [1] arg = > userId matches
userId: user._id,
},
// [2] arg => secret token
process.env.SECRET_TOKEN,
{
// [3] arg => token duration
expiresIn: "24h",
}
),
});
})
.catch((error) => res.status(500).json({ error }));
})
.catch((error) => res.status(500).json({ error }));
};
content_copyCOPY
Comments