title: AngularJS - $rootScope.currentUser not being transferred from profile page to settings page
body: <p>I have a site with Angular/Express/Node/Passport that I am trying to put together. I am setting up angular to watch the <code>$rootScope.currentUser</code> variable, here is some code:</p>
  
  <pre><code>app.run(function ($rootScope, $location, Auth) {
  
      //watching the value of the currentUser variable.
      $rootScope.$watch('currentUser', function(currentUser) {
        // if no currentUser and on a page that requires authorization then try to update it
        // will trigger 401s if user does not have a valid session
        if (!currentUser &amp;&amp; (['/', '/login', '/logout', '/signup'].indexOf($location.path()) == -1 )) {
          Auth.currentUser();
        }
      });
  
      // On catching 401 errors, redirect to the login page.
      $rootScope.$on('event:auth-loginRequired', function() {
        //console.log(currentUser);
        $location.path('/login');
        return false;
      });
    });
  </code></pre>
  
  <p>Using console messages, I have established that the <code>$rootScope.currentUser</code> is not available when I go from the profile page (after login) to the settings page (where you can change your password) - because when the <code>currentUser</code> is watched (above) - it does the <code>Auth.currentUser()</code> function - and gets rejected because <code>$rootScope.currentUser</code> is null.</p>
  
  <p>Here is some more, possibly relevant, code:</p>
  
  <p>Auth.js (where <code>Auth.currentUser()</code> is)</p>
  
  <pre><code>'use strict';
  
  angular.module('groundup')
    .factory('Auth', function Auth($location, $rootScope, Session, User, $cookieStore) {
      $rootScope.currentUser = $cookieStore.get('user') || null;
      $cookieStore.remove('user');
      $rootScope.currentUserSignedIn;
  
      return {
  
        login: function(provider, user, callback) {
          var cb = callback || angular.noop;
          //console.log(user);
          Session.save({
            //provider: provider,
            username: user.username,
            password: user.password,
            //rememberMe: user.rememberMe
          }, function(user) {
            console.log(user);
            $rootScope.currentUser = user;
            $rootScope.currentUserSignedIn = true;
            console.log($rootScope.currentUser);
            $location.path('/profile');
            return cb();
          }, function(err) {
            console.log(err);
            return cb(err.data);
          });
        },
  
        logout: function(callback) {
          var cb = callback || angular.noop;
          Session.delete(function(res) {
              $rootScope.currentUser = null;
              $rootScope.currentUserSignedIn = false;
              console.log($rootScope.currentUser);
              return cb();
            },
            function(err) {
              return cb(err.data);
            });
        },
  
        createUser: function(userinfo, callback) {
          var cb = callback || angular.noop;
          console.log(userinfo);
          User.save(userinfo,
            function(user) {
              $rootScope.currentUser = user;
              $rootScope.currentUserSignedIn = true;
              console.log($rootScope.currentUser);
              $location.path('/profile');
              return cb();
            }
          );
        },
  
        currentUser: function() {
          Session.get(function(user) {
            console.log(user);
            $rootScope.currentUser = user;
          });
        },
  
        changePassword: function(oldPassword, newPassword, callback) {
          var cb = callback || angular.noop;
  
          return User.update({
            oldPassword: oldPassword,
            newPassword: newPassword
          }, function(user) {
            return cb(user);
            $location.path('/');
          }, function(err) {
            return cb(err);
          }).$promise;
        },
  
        removeUser: function(email, password, callback) {
          var cb = callback || angular.noop;
          User.delete({
            email: email,
            password: password
          }, function(user) {
              console.log(user + 'removed');
              return cb();
          }, function(err) {
              return cb(err.data);
          });
        }
      };
    })
  </code></pre>
  
  <p>After <code>Auth.currentUser()</code> it does a get request, which executes this function from the express side:</p>
  
  <pre><code>exports.session = function (req, res) {
    console.log(req.user.user_info + " in session function");
    res.json(req.user.user_info);
  };
  </code></pre>
  
  <p>The problem is on the redirect to the <code>/settings</code> page - because <code>$rootScope.currentUser</code> is being watched, it auto redirects to <code>/login</code> because the <code>Auth.currentUser()</code> fails somewhere. Any help would be greatly appreciated - thanks (look at how $cookieStore is being used in Auth.js - I am not exactly sure how that is working). </p>