I'm building a simple SPA using the MEAN stack and Passportjs for authentication, where users can create, see and answers polls. The page routing is done via angular and $routeProvider. The api calls for creating/managing polls and the signup/login calls are done via express routing. It all works fine, I can create a user and I can also login but I'm now having problems with the flash messages generated by passport when the login or signup fails.
I'm assigning messages to req.flash in my passport.js file but I couldn't find how to send these messages to the client and how to receive and display them. All the examples and Q&A I found were using exclusively express routing and templates as ejs. I don't have anything against them but I'd rather not use them if possible.
Some snippets here:
Return statement to the signup function when it finds the email already exists:
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
Signup routing doen via express. Redirection works correctly and I can see the users in the DB:
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/',
failureRedirect : '/signup',
failureFlash: true
}));
If I was using only express routing and I was using ejs I could implement this snippet - but I'd rather not. The question is: what would be an alternative to this? How can I send the message set in req.flash to the client and how can I retrieve them?
app.get('/signup', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('signup.ejs', { message: req.flash('signupMessage') });
});