【问题标题】:AngularJS - $rootScope.currentUser not being transferred from profile page to settings pageAngularJS - $rootScope.currentUser 没有从个人资料页面转移到设置页面
【发布时间】:2014-07-21 22:41:47
【问题描述】:

我有一个包含 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(Auth.currentUser() 所在的位置)

'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 中的使用方式 - 我不确定它是如何工作的)。

【问题讨论】:

    标签: javascript angularjs session authentication get


    【解决方案1】:

    您的Auth.currentUser() 有一些奇怪的地方,请尝试将其更改为类似的内容以使用User 服务

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

    您的问题也可能来自 cookie 存储问题,Passport 尝试将当前用户保存在其中,但在某处失败(原因是尝试访问未定义的 var 或其他内容)并在 cookie 中存储了一个损坏/空的用户。 所以第一页加载正确,但是当你的应用尝试访问 cookie 时,它​​会中断。

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

    【讨论】:

    • 嘿!感谢您的帮助-我认为您将我推向了正确的方向-我应该使用用户资源而不是会话资源来获取用户信息-问题是-用户信息应该已经在会话中...所以我在做什么应该工作......
    • 如果在请求会话资源时保存了一些不好的东西,那可能会导致您的问题
    • 没有任何错误请求 - 一切正常
    • 我仍然没有修复它,虽然我认为我对我的代码做了一些积极的改变 - 我仍然不确定是什么问题...我将我的 Auth.currentUser 更改为这个...currentUser: function() { Session.get() $rootScope.currentUser = user; },
    • 你有时间聊天吗?
    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 2017-10-26
    • 2011-01-25
    • 2014-10-09
    • 2015-10-08
    • 1970-01-01
    • 2023-03-30
    • 2016-11-26
    相关资源
    最近更新 更多