2

I've been scratching my head for this one for hours now yet still haven't found the cause or solution.

Basically I have an Angular.js frontend that tries to log users in (with ngCookies on Angular.js 1.3.15) by communicating with an authentication API endpoint in the backend. Upon success, my code should redirect the user to another page.

So the login code looks something like:

auth.login($scope.user).success(function() {
  $state.go('overview');
});

where auth is a service that has a login method which simply sends an POST request to the backend. $state.go('overview') will do the redirect. This code should be ok as I get HTTP200 back from the server given I supply the correct user credentials. However, the browser won't redirect me to the overview page because it still thinks I'm not logged in unless I refresh the page.

The code that checks if the current user is logged in uses a helper method called getToken(), where I use ngCookies to retrieve a cookie named USER_SESSION. And this method is where I think the problem is.

The method itself is simply

auth.getToken = function() {
  return $cookies.USER_SESSION;
};

However it always returns undefined even when the USER_SESSION cookie exists in the browser.

Some testing I did (on a remote testing server so accessing cookies in localhost shouldn't be a concern):

auth.getToken = function() {
  console.log($cookies);
  console.log($cookies.USER_SESSION);
  return $cookies.USER_SESSION;
}

The results of console.log:

// Before login:
Object {...} // No USER_SESSION, this is good
undefined

// After hitting the login button, browser sends AJAX request,
// server returns HTTP200 response with Set-Cookie in the header:
Object {USER_SESSION: ""aa21fcd..."", ...} // UESR_SESSION exists, good
undefined // ???

// After refreshing the page:
Object {USER_SESSION: ""09b4tk..."", ...}
"09b4tk..."

Are the double quotes the problem here? I thought so initially, but after refreshing the page, $cookies returns the same thing (same double quotes but a different USER_SESSION value) and $cookies.USER_SESSION would have the correct value, and user would be on the overview page.

Does this mean Set-Cookie in the HTTP response header needs a page refresh to take effect?

Thanks!

P.S. Same problem occurs with cookieStore.

4

3 回答 3

1

我有同样的问题,我无法用 ngCookies 解决这个问题。我决定改用本地存储。

我使用这个模块: https ://github.com/grevory/angular-local-storage

而且,根据您显示的代码,本地存储将更适合您的应用程序。请参阅:本地存储与 Cookie

于 2015-04-25T17:36:34.693 回答
0

因为界面变了。您现在需要将 .get() 和 .set() 与 $cookies 一起使用。

    $cookies.set('USER_SESSION', 'your value here');

    auth.getToken = function() {
        return $cookies.get('USER_SESSION');
    }
于 2015-06-10T09:25:18.987 回答
0

因此,我也为此苦苦挣扎了一两天,因为我正在处理登录 cookie。我使用的示例 where done with $cookieStore,现在自然不推荐使用了,所以我很快切换到$cookies,认为就是这样。

好吧,事情就是这样。使用 时$cookies,请务必注意$cookies.put和之间的区别$cookies.putObject。当然,这同样适用于$cookies.getand $cookies.getObject

我愚蠢地认为我的对象无论如何都会起作用,但不,结果证明这就是我最终获得undefined.

现在,对于您的问题,我将假设这'USER_SESSION'是一个对象,因此您遇到了这个问题。否则,您似乎也没有使用 .put() 或 .get() 。所以现在试试这个。

//if your cookie is an object

$cookies.putObject('USER_SESSION', cookieObject)

auth.getToken = function() {
    return $cookies.getObject('USER_SESSION');
}


//if your cookie is not an object

$cookies.put('USER_SESSION', 'cookie')

auth.getToken = function() {
    return $cookies.get('USER_SESSION');
}
于 2018-04-06T12:00:09.497 回答