角JS返回用户自动登录 [英] angular js returning user autologin

查看:172
本文介绍了角JS返回用户自动登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过令牌的认证系统。用户用电子邮件和密码登录并返回并保存在Cookie令牌。现在,如果用户关闭浏览器或标签,并返回到网站,然后用户被认证与在cookie令牌,但是,可以采取相当几毫秒,如果他们返回到安全网站例如他们的用户简档,以及应用程序是不够的快速加载初始用户数据和将其标记为登录,那么他们将被重定向到登录页面。

I have an authentication system via tokens. The user logs in with email and password and the token is returned and saved in a cookie. Now if the user closes the browser or tab and returns to the site, then the user is authenticated with the token in the cookie, however, that can take a quite a few milliseconds and if they return to a secure site such as their user profile, and the app is not fast enough to load the initial user data and marks them as logged in, then they are redirected to the login page.

我最初的想法只是把用户数据中为每个请求,这里的令牌是有效的反应,但会造成大量不必要的数据流量。

My first idea was just to put the user data in the response for every request, where the token is valid, but that would create lots of unnecessary data traffic.

有没有更好的解决办法?

Is there a better solution?

推荐答案

使用解析航线上要等到用户信息被加载。因此,无论重定向到登录页面或允许访问路径取决于你的用户负载处理程序的处理程序。例如:

Use resolves on routes to wait till user info is loaded. So whether to redirect to log in page or allow the route access depends on handler of your load user handler. For example:

function requiresLogin(){
return ["$auth",function($auth){
return $auth.verifiedUser();
}];
}

$routeProvider.when("...",{
....,
resolve:{
user:requiresLogin()
}
});

在你的$身份验证服务:

In your $auth service:

angular.module("app").factory("$auth", ["$q", "$http", function($q,$http){
var loaded, loggedIn = false;

//load user 
loaded = $http.get(....).then(function(result){
loggedIn = result.loggedIn;
});

return {
verifiedUser:function(){
return loaded.then(function(){
    if(loggedIn)
return true;
else
return $q.reject();
});
}
};
}]);

未经测试code,只给如何去这一个想法。

Untested code, just to give an idea on how to go about this.

我们无法提供一个明确的答案与更多code。

We can not provide an explicit answer with out more code.

这篇关于角JS返回用户自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆