Authentication and Session PASSPORTJS

Here is the next part of the implementation of passport.js and express session.  To create a simple authentication with session, i would recommend the usage of these 3 libraries

  1. connect-ensure-login
  2. express-session
  3. passport

Main.js

app.post(‘/login’, passport.authenticate(‘local’, {
  successReturnToOrRedirect: ‘/dashboard’,
  failureRedirect: ‘/login’
 }));
Route.js
router.get(‘/dashboard’,connectEnsureLogin.ensureLoggedIn(),students.dashboard);
router.get(‘/applyuni’,connectEnsureLogin.ensureLoggedIn(),students.applyuni);
router.post(‘/applyuni’,connectEnsureLogin.ensureLoggedIn(),students.applyuniform); //include validator
students.js
exports.dashboard = async (req,res)=>{
  const info = {
    ‘name’:req.user.name,
    ‘phone’: req.user.phone,
    ‘username’: req.user.username
  };
  res.render(‘./pages/dashboard’,{info});
};
The library ‘connect-ensure-login’ is super good. It really works well with passport.js. Once authenticated, the session will be stored. to access the session, just use the following sample command. “req.user.username”  With the session installed, you are able to create authorisation easily