June 2018
Intermediate to advanced
348 pages
8h 19m
English
We have already defined the route that will be responsible to make our logic available as a REST endpoint, so the only step required is to call our logIn function. Go ahead and apply the following change:
...const generateToken = (userData) => { return jwt.sign(userData, "s3cret", { expiresIn: '3h' })}api .route('/auth') .post((req, res, next) => { let { username, password } = req.body let token = logIn(username, password) if (token) { res.send(token) } else { next(new Error("Authentication failed")) } })module.exports = api
First, we extract the username and password from the req.body object. After that, we call the logIn function and host the result in the token variable. If the token is not null, we ...
Read now
Unlock full access