AngularJS - $rootScope.当前用户未从配置文件页面转移到设置页面



我有一个网站与Angular/Express/Node/Passport,我正试图放在一起。我正在设置角来观察$rootScope.currentUser变量,这里是一些代码:

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 && (['/', '/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;
    });
  });

使用控制台消息,我已经确定,当我从配置文件页面(登录后)进入设置页面(您可以更改密码)时,$rootScope.currentUser不可用-因为当currentUser被监视时(上面)-它执行Auth.currentUser()功能-并且因为$rootScope.currentUser为空而被拒绝。

下面是一些可能相关的代码:

Auth.js (where Auth.currentUser() is)

'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);
        });
      }
    };
  })

Auth.currentUser()之后,它做了一个get请求,从express端执行这个函数:

exports.session = function (req, res) {
  console.log(req.user.user_info + " in session function");
  res.json(req.user.user_info);
};

问题是在重定向到/settings页面上-因为$rootScope.currentUser正在被监视,它自动重定向到/login,因为Auth.currentUser()在某个地方失败了。任何帮助都将非常感激-谢谢(看看$cookieStore是如何在Auth.js中使用的-我不太确定这是如何工作的)。

你的Auth.currentUser()有点奇怪,试着把它改成这样来使用User服务

currentUser: function () {
    return User.get();
}

您的问题也可能来自cookie存储问题,Passport试图将当前用户保存在其中,但在某处失败(由于尝试访问未定义的var或其他东西),并在cookie中存储损坏/空用户。所以第一页是正确加载的,但是当你的应用程序试图访问cookie时,它会崩溃。

我还建议您查看lib目录中的middleware.js文件,看看cookie保存是否没有问题,例如如果您想将所有用户对象放入其中而不仅仅是userInfo。在本例中,您必须稍微修改session函数以输出整个用户。

相关内容

最新更新