0

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?

karlkurzer
  • 115
  • 9

1 Answers1

0

I think you are meant to use a Angular service to store variables that are to be shared amongst controllers. Sorry, I guess I'm trying to nudge you out of your preferred solution to use the Window object to store state.

Some sample code from a common services module

//var com_serv = angular.module('common.services')
com_serv.service('sharedProperties', function () {
    var property = 'Not yet set';

    return {
        getProperty: function () {
            return property;
        },
        setProperty: function (value) {
            property = value;
        }
    };
});

Related SO questions

how-can-i-pass-variables-between-controllers

how-to-pass-dynamic-data-from-one-module-controller-to-another-module-controller

Community
  • 1
  • 1
S Meaden
  • 8,050
  • 3
  • 34
  • 65
  • hey meaden, inside angular I am using a factory to retrieve the user object from the window, which works, but also only **after** the page refresh: `angular.module('users').factory('Authentication', [ function() { // Use the rendered user object this.user = window.user; // Return the authenticated user data return { user: this.user }; } ]);` – karlkurzer Aug 12 '15 at 10:02
  • I'm on a different track from you. You seem to want to use the window to store state. I have posted some samples and also links to related SO question that take a different approach. – S Meaden Aug 12 '15 at 10:08
  • Hey meaden, thanks again. I guess my question might not be that well phrased. The problem is that I need to refresh the page in order to get the updated values, I believe it might have something to do with the DOM not rendering the change immediately and that's why the digest cycle isn't catching it either, but I am not sure... – karlkurzer Aug 12 '15 at 10:25
  • Well sorry to go on again about how my approach is better ... but... I am using Angular to build a Single Page Application where page refreshes are to be avoided. So I always look for the Angular prescribed solution. – S Meaden Aug 12 '15 at 10:28