I have a problem with my login routine using passport js, express and angular. While the login as such works smoothly I want to use the window object to store the current logged in user for authentication purposes.
After username and password are validated using passport I am using the successRedirect, that triggers index.render
// Set up the 'signin' routes
app.route('/signin')
.post(passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/signin',
failureFlash: true
}));
exports.render = function(req, res) {
// Use the 'response' object to render the 'index' view with a 'title' and a stringified 'user' properties
console.log('Window Object:',req.user);
res.render('index', {
title: 'Test',
user: JSON.stringify(req.user)
});
};
Once I login to the application the window.user object turns from null to the following (index.ejs).
<!-- Render AngularJS views -->
<header data-ng-include="'header.client.view.html'"></header>
<section data-ui-view></section>
<div id="contactform" data-ng-include="'contactform.client.view.html'"></div>
<footer data-ng-include="'footer.client.view.html'"></footer>
<!-- Render the user object -->
<script type="text/javascript">
window.user = {"_id":"55cb06e523d7d6680d14c215","provider":"local","firstName":"Karl","lastName":"Karl","email":"karl.karl@test.com","__v":0,"created":"2015-08-12T08:42:13.807Z","active":true,"fullName":"Karl Karl","id":"55cb06e523d7d6680d14c215"};
</script>
Now I have been very unsuccessful in retrieving the window.user object in my angular controller as it is seen as null as long as I haven't done a page refresh.
<!-- Render the user object -->
<script type="text/javascript">
window.user = null;
</script>
So the question is now, how do I retrieve the window object, as I want to update my angular view accordingly, showing in the header that the user has been logged in?