所有
我只是想在AngularJS应用程序中将sessionStorage中的用户对象存储起来。如果我在Chrome或FF调试器中逐步执行此操作,则sessionStorage永远不会被设置。这是我的角度服务代码:
// Authentication/authorization Module
stonewall.authModule = angular.module("authModule", [
// Module dependencies
"ngStorage"
]);
// Authentication/authorization service tracks the current user
stonewall.authModule.service('authService', function ($localStorage, $sessionStorage) {
// Initialize current user
var currentUser = {};
restoreSession();
// Declare storage type-this may change if user selects
// "Keep me signed in"
var storageType = {};
// Return the current user object
this.getCurrentUser = function () {
return currentUser;
};
// Returns whether there is a currently authorized user
this.userAuth = function() {
return currentUser.sid != "";
};
// Logout function, initializes the user object
this.logout = function() {
currentUser = {
sid: "",
status: 0,
pswLastSet: 0,
id: "",
sigUID: "",
sig: ""
};
//persistSession();
};
// Login
this.login = function(user, subj) {
if (user == null) return;
currentUser = {
sid: user.Principal.SId,
status: user.Principal.ControlStatus,
pswLastSet: new Date(user.Principal.PasswordLastSet),
id: user.Identity.Id.DN,
sigUID: user.Identity.Certificates[0].UID,
sig: stonewall.hash(user.Principal.SId + subj.pswd),
};
persistSession();
};
// Persist to session storage
function persistSession() {
$sessionStorage.currentUser = currentUser;
};
// Restore session
function restoreSession() {
currentUser = $sessionStorage.currentUser;
if (currentUser == null) {
// Initialize to empty user
currentUser = {
sid: "",
status: 0,
pswLastSet: 0,
id: "",
sigUID: "",
sig: ""
};
}
};
});
并且,这是一个显示我的FF调试会话的screencap。你可以看到在调用persistSession之后$ sessionStorage有了我的用户。
但是,如果我切换到DOM检查器,sessionStorage中没有项目......
任何帮助一如既往地受到赞赏。
答案 0 :(得分:4)
您确定以正确的方式使用angular的sessionStorage吗? 会话存储是$ window对象的属性,因此我不知道你是否制作了自己的服务包装器或类似的东西? 无论如何,这是一个codepen,它显示了我自己使用的另一种方法,使用$ window.sessionStorage:http://codepen.io/chrisenytc/pen/gyGcx