页面刷新后返回的AngularJS cookie数据未定义

时间:2016-10-13 19:37:43

标签: angularjs cookies

我正在使用AngularJS进行Web开发项目,因为我试图将用户会话存储在cookie中,因此他们无法访问登录页面或注册页面已经登录,还有其他各种原因。

似乎正在发生的事情是我将用户数据存储到cookie中,并且我使用console.log()来返回存储在cookie中的数据,并且存储过程似乎正在运行。但是,当我尝试访问登录屏幕时,我仍然可以这样做。

因此,出于调试目的,我添加了另一个console.log()来检查登录页面加入时存储在cookie中的数据,并返回为undefined。

是否有人知道我的代码中导致数据被转储的问题是什么?

  function login(email, password, callback) {
        return $http.post('/api/v1/auth/login/', {
                email: email,
                password: password
            }).then(function(data, status, headers, config) {
                Authentication.setAuthenticatedAccount(data.data);
                window.location = '/:username';
            }, function (response) {
                var response;
                response = { success: false, message: 'Username or password is incorrect' };
                callback(response);
                console.error('Epic failure!');
            });
 /**
 * @name getAuthenticatedAccount
 * @desc Return the currently authenticated account
 * @returns {object|undefined} Account if authenticated, else `undefined`
 * @memberOf hrlite.authentication.services.Authentication
 */

function getAuthenticatedAccount() {
    if (!$cookies.authenticatedAccount){
        return;
    }

    return $cookies.getObject('authenticatedAccount');
}
/**
 * @name isAuthenticated
 * @desc Check if the current user is authenticated
 * @returns {boolean} True is user is authenticated, else false.
 * @memberOf hrlite.authentication.services.Authentication
 */
function isAuthenticated() {
    console.log("account:" + getAuthenticatedAccount());

    return !!$cookies.authenticatedAccount;
}

 /**
 * @name setAuthenticatedAccount
 * @desc Stringify the account object and store it in a cookie
 * @param {Object} user The account object to be stored
 * @returns {undefined}
 * @memberOf hrlite.authentication.services.Authentication
 */

function setAuthenticatedAccount(account) {

    $cookies.putObject('authenticatedAccount', account);
}

0 个答案:

没有答案