Implementing the User Login Route
Listing 26.5 implements the login route. First, the handler finds the user by username, then it compares the stored hashed password with a hash of the password sent in the request. If the passwords match, the user session is regenerated using the regenerate() method. Notice that req.session.user and req.session.username are set in the regenerated session.
Listing 26.5 users_controller.js-login: Implementing the route for user login for the Express server
24 exports.login = function(req, res){25 User.findOne({ username: req.body.username })26 .exec(function(err, user) {27 if (!user){28 err = 'User Not Found.';29 } else if (user.hashed_password ===30 hashPW(req.body.password.toString())) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access