This is the second part on my Passport tutorial. Please read first part if you haven’t.
Import session, express-validator, connect flash into app.js
1234567891011121314151617181920212223242526272829303132333435363738394041var session = require('express-session');var flash = require('connect-flash');var expressValidator = require('express-validator');// Express Sessionapp.use(session({secret: 'secret',saveUninitialized: true,resave: true}));// Express Validatorapp.use(expressValidator({errorFormatter: function(param, msg, value) {var namespace = param.split('.'), root = namespace.shift(), formParam = root;while(namespace.length) {formParam += '[' + namespace.shift() + ']';}return {param : formParam,msg : msg,value : value};}}));// Connect Flashapp.use(flash());// setup local variables so we can use it anywhere in our appapp.use(function (req, res, next) {res.locals.success_msg = req.flash('success_msg');res.locals.error_msg = req.flash('error_msg');res.locals.error = req.flash('error');res.locals.user = req.user || null;next();});Add flash and validation into routes/users.js
|
|
Implement the flash message to sign in & sign up views.
12345678910111213//signin.ejs<% if(success_msg) { %><div class="success"><%= success_msg %></div><% } %>//signup.ejs<% if(errors){ %><% errors.forEach(function(error){ %><div class="error"><%= error.msg %></div><% }) %><% } %>
This is it on how to implement passportJS to your express app. A lot of code is the same from the library’s document. I recommend you going to each individual library and read their docs. This post is to show you on how to implement them together.