0
我正試圖在前端維護會話。但是,我發現無論何時刷新瀏覽器或更改window.location
。該cookie消失。
angular
.module('myservices')
.factory('myAuthentication', Authentication);
Authentication.$inject = ['$cookies', '$http', '$q', '$rootScope'];
function Authentication($cookies, $http, $q, $rootScope){
var Authentication = {
login : login,
getAuthenticatedAccount : getAuthenticatedAccount,
isAuthentiate : isAuthentiate,
setAuthenticateAccount : setAuthenticateAccount,
unAuthenticate : unAuthenticate,
};
return Authentication;
function login(email, password){
$http.post('login/', {
email: email, password: password
}).then(loginSuccess, loginError);
function loginSuccess(response){
Authentication.setAuthenticateAccount(response.data);
$rootScope.$broadcast('login', "login");
// changing window location will delete the cookies
window.location = '/';
}
function loginError(response){
console.log('error');
}
}
function getAuthenticatedAccount(){
if (!$cookies.authenticatedAccount){
return ;
}
return JSON.parse($cookies.authenticatedAccount);
}
function isAuthentiate(){
return !!$cookies.authenticatedAccount;
}
function setAuthenticateAccount(account){
$cookies.authenticatedAccount = JSON.stringify(account);
}
}
我在Firefox和Safari瀏覽器測試這一點,我相信,我測試了氾濫,在Firefox檢查禁用緩存。
有沒有其他角度的設置保持cookie的持久性?
哪個版本的AngularJS?版本1.4+建議使用[$ getters和setter of $ cookies](https://docs.angularjs.org/api/ngCookies/service/$cookies)。除此之外,還有'putObject'和'getObject' APIs,它們不需要使用JSON.stringify和parse。 –
http://jsfiddle.net/arunpjohny/rb08wgha/1/ –