I use Node with express and handlebars. I have a login form and the user should receive a login error message. My code is as follows: Verification (using passport):
...
else if (password != user.password) {
return done(null, false, req.flash('message', 'Wrong password'));
...
In the routes, I got this:
app.post('/sign-in', passport.authenticate('local', {
successRedirect : '/',
failureRedirect : '/sign-in',
failureFlash : true
}));
Then, to display my handlebars template,
app.get('/sign-in', function(req, res) {
res.render("signin.handlebars", {layout: 'users.handlebars', action: 'Sign in', message: req.flash('message'),
csrf: 'CSRF token goes here' });
})
The problem is that the flash message does not appear if necessary when entering the wrong password.
Edit: My express installation:
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('models', __dirname + '/models');
app.use(express.static(__dirname + '/public'));
app.use(cookieParser());
app.use(expressSession({secret:'somesecrettokenhere', resave: true,
saveUninitialized: true, }));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(flash());
app.use(morgan("dev"));
app.disable('x-powered-by');
app.use(function(err, req, res, next) {
res.status(err.status || 500);
});