At this point, we have enough code to configure secure access to our application. However, we still need to add a few more lines to the login and sign-up forms to make them work properly:
Open server/routes/index.js and add the following lines after the login GET route:
/* POST login */
router.post('/login', passport.authenticate('local-login', {
//Success go to Profile Page / Fail go to login page
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
}));
Add these lines after the signup GET route:
/* POST Signup */
router.post('/signup', passport.authenticate('local-signup', {
//Success go to Profile Page / Fail go to Signup page
successRedirect : '/profile',
failureRedirect : '/signup',
failureFlash : true
}));
Now let's add a simple function to check whether the user is logged in; at the end of server/routes/index.js, add the following code:
/* check if user is logged in */
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/login');
}
Let's add a simple route to a logout function and add the following code after the isLoggedIn() function: