I'm adapting some code that presupposes a traditional non-SPA page load, and displays flash messages to the user generated by connect-flash middleware. It does this by passing in an object that is rendered in a EJS file.
My app is angular and I have no templating engine on the backend, and would like to avoid using one. How can I make angular aware of these flash messages?
Here is the context: passportjs redirects on error to /signup
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
Thanks for any help!
My solution was to create an EJS page whose only purpose was to extract the connect-flash data and redirect the browser to the specified route with data appended as querystring.
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.get('/signup', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('redirect.ejs', {redirect_url: "/?route=/signup&message="+req.flash('signupMessage')});
});
redirect.ejs
<html>
<meta http-equiv="refresh" content="0;<% if (redirect_url) { %><%=redirect_url %><% } %>" />
</html>
angular controller
app.controller("Index", function ($scope, $route, $routeParams,$location) {
var route= $routeParams.route, msg=$routeParams.message;
if (route) {
if ($location.$$search.route) {
delete $location.$$search.route;
$location.$$compose();
}
console.log(route, msg);
$location.path(route);
}
});
It could have been worse.