Javascript 如何在js中的cookie中存储登录会话id?
我有下面的代码,其中我将用户的登录凭据存储在本地存储中,以及他的权限,无论他是否是管理员Javascript 如何在js中的cookie中存储登录会话id?,javascript,angularjs,session,cookies,Javascript,Angularjs,Session,Cookies,我有下面的代码,其中我将用户的登录凭据存储在本地存储中,以及他的权限,无论他是否是管理员 .success(function (user) { localStorageService.set('localStorageUser', user.username); localStorageService.set('localStorageUserRole', user.authorities[0].authority); $
.success(function (user) {
localStorageService.set('localStorageUser', user.username);
localStorageService.set('localStorageUserRole', user.authorities[0].authority);
$rootScope.showMenu = true;
$rootScope.user = user.username;
if(localStorageService.get('localStorageUserRole') === 'ROLE_ADMIN'){
$rootScope.isAdmin = true;
}
else {
$rootScope.isAdmin = false;
}
d.resolve();
})
我想知道如何将用户登录会话id存储在cookie中,以及如何在AngularJS中设置cookie的过期日期。AngularJS已经提供了处理cookie的包装 您可以访问此页面了解如何将ngCookies插件添加到您的项目中: 首先在HTML中包含angular-cookies.js: 你可以 从以下位置下载此文件: 谷歌CDN //ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-cookies.js 然后通过将模块作为依赖项添加到应用程序中来加载该模块 模块: 模块'app',['ngCookies'] 你准备好了吗 开始 $cookie的使用非常简单:
$cookies.put('user', user.username);
var username = $cookies.get('user');
//You can set the expired time with the third params
var today = new Date();
var expired = new Date(today);
expired.setDate(today.getDate() + 1); //Set expired date to tomorrow
$cookies.put('user', user.username, {expires : expired });
关于$cookies的更多信息:AngularJS已经提供了一个用于处理cookies的包装器 您可以访问此页面了解如何将ngCookies插件添加到您的项目中: 首先在HTML中包含angular-cookies.js: 你可以 从以下位置下载此文件: 谷歌CDN //ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-cookies.js 然后通过将模块作为依赖项添加到应用程序中来加载该模块 模块: 模块'app',['ngCookies'] 你准备好了吗 开始 $cookie的使用非常简单:
$cookies.put('user', user.username);
var username = $cookies.get('user');
//You can set the expired time with the third params
var today = new Date();
var expired = new Date(today);
expired.setDate(today.getDate() + 1); //Set expired date to tomorrow
$cookies.put('user', user.username, {expires : expired });
关于$cookies的更多信息:为什么要创建一个“今天”?为什么不将过期日期设置为新日期;?你为什么要创造一个今天?为什么不将过期日期设置为新日期;?