prevent AngularJS控制器调用如果非 [英] Prevent AngularJS Controller call if unauthenticated

查看:100
本文介绍了prevent AngularJS控制器调用如果非的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有路线变化的基本认证检查这只是检查一个关键的sessionStorage / VAL的存在。不过,我已经注意到,如果未认证用户导航到一个页面禁谁的控制器包含一个AJAX调用,AJAX调用仍然发生在后台,即使禁止页面没有加载,用户被重定向,而不​​是登陆。为了解决这个问题,我已经添加了每个控制器相同的验证检查。这已成为一个有点乏味,我想知道是否有更好的方法,还是有办法之前控制器的方法,我可以在全球范围检查认证中运行。下面是路由变化在auth功能:

  app.run(函数($ rootScope,$位置的AuthenticationService){    $ rootScope.clearAlerts =功能(){
        $ rootScope.alerts = []
    }    $ rootScope.credentials = {
        用户名: ,
        密码:
    };    $ rootScope.goto =功能(URL){
        $ location.path(URL);
    }    $ rootScope.authenticated = authenticationService.isLoggedIn()
    $ rootScope.logOut =功能(){
        authenticationService.logOut();
    }
    VAR publicRoutes = ['/登录'];    $ rootScope。在$('$ routeChangeStart',函数(事件,接下来,电流){
        $ rootScope.credentials.from = $ rootScope.desiredPath || '/登录'
        $ rootScope.authenticated = authenticationService.isLoggedIn();
        如果(_(publicRoutes)。载有($ location.path())及!&安培;!$ rootScope.authenticated){
            $ rootScope.desiredPath = $ location.path();
            $ location.path('/登录');
        }
    })
})


解决方案

我建议你应该在服务器端查看验证和客户端返回相应的消息

下面是url帮助
http://www.espeo.pl/2012/02/26 /认证在angularjs应用

和写入全局处理程序客户端,检查其

下面是一个示例code:

  angular.module('对myApp',['myApp.filters','myApp.services','myApp.directives'],功能($ routeProvider,$ locationProvider,$ httpProvider ){    VAR拦截= ['$ rootScope','$ Q'功能(范围,$ Q){        函数成功(响应){
            返回响应;
        }        功能错误(响应){
            VAR状态= response.status;            如果(状态== 401){
                window.location的=./index.html;
                返回;
            }
            // 除此以外
            返回$ q.reject(响应);        }        复位功能(承诺){
            返回promise.then(成功,错误);
        }    }];
    $ httpProvider.responseInterceptors.push(拦截);

I have a basic authentication check on route change which simply checks the existence of a SessionStorage key/val. However, I have noticed that if an unauthenticated user navigates to a forbidden page who's controller contains an AJAX call, the AJAX call still happens in the background even though the forbidden page doesn't load and the user is instead redirected to login. To get around this, I have added the same authentication check in every controller. This is becoming a bit tedious and I was wondering if there is a better way, or a way I can globally check authentication before a controller's methods are run. Here is the auth function on route change:

app.run(function ($rootScope, $location, authenticationService) {

    $rootScope.clearAlerts = function(){
        $rootScope.alerts = []
    }

    $rootScope.credentials = {
        username: "",
        password: ""
    };

    $rootScope.goto = function(url){
        $location.path(url);
    }

    $rootScope.authenticated = authenticationService.isLoggedIn()
    $rootScope.logOut = function () {
        authenticationService.logOut();
    }
    var publicRoutes = ['/login'];

    $rootScope.$on('$routeChangeStart', function (event, next, current) {
        $rootScope.credentials.from = $rootScope.desiredPath || '/login'
        $rootScope.authenticated = authenticationService.isLoggedIn();
        if (!_(publicRoutes).contains($location.path()) && !$rootScope.authenticated) {
            $rootScope.desiredPath = $location.path();
            $location.path('/login');
        }
    })
})

解决方案

I suggest you should check authentication on server side and return appropriate message on client side

Below is the url help http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application

and Write global handler in client side to check it

Below is a sample code:

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives'], function ($routeProvider, $locationProvider, $httpProvider) {

    var interceptor = ['$rootScope', '$q', function (scope, $q) {

        function success(response) {
            return response;
        }

        function error(response) {
            var status = response.status;

            if (status == 401) {
                window.location = "./index.html";
                return;
            }
            // otherwise
            return $q.reject(response);

        }

        return function (promise) {
            return promise.then(success, error);
        }

    }];
    $httpProvider.responseInterceptors.push(interceptor);

这篇关于prevent AngularJS控制器调用如果非的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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