April 2020
Intermediate to advanced
716 pages
18h 55m
English
The API endpoint to sign-in a user is declared in the following route.
mern-skeleton/server/routes/auth.routes.js:
router.route('/auth/signin').post(authCtrl.signin)
When the Express app gets a POST request at '/auth/signin', it executes the signin controller function.
mern-skeleton/server/controllers/auth.controller.js:
const signin = async (req, res) => { try { let user = await User.findOne({ "email": req.body.email }) if (!user) return res.status('401').json({ error: "User not found" }) if (!user.authenticate(req.body.password)) { return res.status('401').send({ error: "Email and password don't match." }) } const token = jwt.sign({ _id: user._id }, config.jwtSecret) res.cookie('t', token, { expire: new Date() + 9999 }) return res.json({ ...Read now
Unlock full access