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 })); };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter