AngularJS-重定向到登录页面和会话ID的持久性 [英] AngularJS - Redirect to Login page and Persistence of Session ID

查看:59
本文介绍了AngularJS-重定向到登录页面和会话ID的持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来完成这两件事,首先,如果未找到SessionID,我想将用户重定向到登录页面,其次,我想听听您关于仅将会话ID保留在内存中的意见(没有cookie) ).

I am looking for a way to do these two things, first I want to redirect the user to a login page if no SessionID is found and second I would like to hear your opinion about persisting session ID in memory only (no cookies).

我想到的重定向解决方案是:

The solution I came up with for the redirect is:

1-创建一个名为OAuth的服务,该服务将检查SessionID是否存在,如果不存在,则重定向到登录页面,该服务还负责登录和注销方法.

1 - Create a service called OAuth that will check if SessionID exists and if not, redirects to login page, the service is also responsible for the login and logout methods.

app.factory('OAuth', ['$http', function ($http) {

    var _SessionID = '';

    return { 
        login: function () {
            //Do login ans store sessionID in var _SessionID
        },

        logout: function () {
            //Do logout
        },

        isLoggedIn: function () {
            if(_SessionID) {
                return true;
            }
            //redirect to login page if false
        }
    };

}]);

2-在每个控制器中注入新的OAuth服务,并检查用户isLoggedIn

2 - Inject the new OAuth service in each controller and check if user isLoggedIn

app.controller('myCtrl', ['$scope', 'OAuth', function ($scope, OAuth) {

    //check if user is logged
    OAuth.isLoggedIn();

}]);

问题:

1-isLoggedIn()方法将在所有控制器中调用,因此我想知道是否有一种方法可以不必注入服务并在每个控制器中调用它.

1 - The isLoggedIn() method will be called in all controllers, so I wonder if there is a way to do this without having to inject the service and call it in each controller.

2-我没有使用cookie来存储sessionID,而是将其保存在OAuth的_SessionID变量中,并针对每个请求将其发送到服务器.这是可行/安全的方法吗?你能给我一些想法吗?

2 - Instead of having a cookie to store the sessionID I want to save it in OAuth's _SessionID variable and for each request send it to the server. Is this a viable/secure approach? Can you give me some ideas for that?

谢谢!

推荐答案

我会在这里,Witold创建了这个很酷的拦截器,它可以根据http响应进行工作.我使用它,它真的很有帮助.

I would start here, Witold has created this cool interceptor that works off of http responses. I use it and its been really helpful.

这篇关于AngularJS-重定向到登录页面和会话ID的持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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