如何使用Angular.js继续刷新后登录状态 [英] how to keep login status after refresh using Angular.js
问题描述
我在使用Angular.js一个单页普通的应用程序,在用户登录后,如果做浏览器刷新,符号信息没有了,地位的标志复位,我再次登录,怎么能我一直在日志中状态事件在浏览器中做一个刷新?
I made an one-page regular application using Angular.js, after a user signs in, if do a refresh in browser, the sign info is gone, sign in status is reset, I have to sign in again, how can I keep the log in status event doing a refresh in browser?
感谢您的帮助。
推荐答案
您可以使用AngularJS服务的 $的CookieStore 。
You can use AngularJS service $cookieStore.
但在AngularJS 1.0.7-你不能设置路径或Cookie有效期,你将需要实现自己的服务。
But in AngularJS 1.0.7- you cannot set the path or the cookie duration, you will need to implement your own service.
一些额外提示:
- 用于AngularJS 1.0.7-你需要从这里下载库
-for AngularJS 1.0.7- you need to download the library from here
- 你需要注入服务$的CookieStore在控制器/服务
-you need to inject the service $cookieStore in your controller/service
- 你应该建立一个全球性的服务,检查/套/删除会话cookie
-you should create a global service that checks/sets/deletes the session cookie
在使用服务的一些起点,例如:
Some starting point example on using a service:
app.service('global', function($cookieStore, $location, $filter) {
var globalService = {};
globalService.user = null;
globalService.isAuth = function (){
if (globalService.user == null) {
globalService.user = $cookieStore.get('user');
}
return (globalService.user != null);
};
globalService.setUser = function(newUser) {
globalService.user = newUser;
if (globalService.user == null) $cookieStore.remove('user');
else $cookieStore.put('user', globalService.user);
};
globalService.getUser = function() {
return globalService.user;
};
return globalService;
});
您登录调用SETUSER来保存用户的cookie(可能由服务器返回的一些象征性的)之后。
然后,你应该发送的鉴别令牌数据为每一个需要登录的用户请求。
After your login call setUser to save the user (maybe some token returned by the server) in a cookie. Then you should send the authentification token data for every request that requires a logged in user.
该令牌可以是一个PHP会话(如果使用PHP)和会话可以在服务器上进行恢复。
The token can be a PHP session (if using PHP) and the session can be restored on the server.
这篇关于如何使用Angular.js继续刷新后登录状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!