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
- connect-ensure-login
- express-session
- passport
Main.js
app.post(‘/login’, passport.authenticate(‘local’, {successReturnToOrRedirect: ‘/dashboard’,failureRedirect: ‘/login’}));Route.jsrouter.get(‘/dashboard’,connectEnsureLogin.ensureLoggedIn(),students.dashboard);router.get(‘/applyuni’,connectEnsureLogin.ensureLoggedIn(),students.applyuni);router.post(‘/applyuni’,connectEnsureLogin.ensureLoggedIn(),students.applyuniform); //include validatorstudents.jsexports.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