AngularJS身份验证问题与查询参数? [英] AngularJS Authentication issue with query parameter?
问题描述
要,我使用$ locationChangeStart处理身份验证。我的code是工作的罚款在 routeProvider 不包含任何参数或查询字符串。如果航线提供者包含任何查询参数,我的code未正常工作。
To handle authentication, i am using $locationChangeStart. My code is working fine for the routeProvider which does not contains any parameter or query string. If route provider contains any query parameter,my code is not working fine.
佛
app.config(['$routeProvider', function($routeProvider,$locationProvider) {
$routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'AuthController',auth:false});
$routeProvider.when('/register', {templateUrl: 'partials/signup.html', controller: 'AuthController',auth:false});
$routeProvider.when('/', {templateUrl: 'partials/:id', controller: 'TestCntrl',auth:false});
$routeProvider.otherwise({redirectTo: '/login'});
}]).run(function($rootScope, $route, $location, AutenticationService) {
$rootScope.$on('$locationChangeStart', function(ev, next, current) {
var nextPath = $location.path();
var nextRoute = $route.routes[nextPath]
if(nextRoute.auth && !AutenticationService.isLoggedIn()){
$location.path("/login");
// event.preventDefault();
}
});
})
在上面的code nextRoute 为映射URL检查并获取财产的权威性,这是在$ routeProvider.But present如果URL中包含查询参数nextRoute.auth给出定义。因为如果此身份验证是行不通的。
In the above code nextRoute checks for the mapping url and fetches property auth, which is present in the $routeProvider.But if url contains query parameter nextRoute.auth gives undefined. Because if this authentication is not working.
任何想法,如何处理身份验证,如果URL中包含查询字符串
Any idea, how to handle authentication, if URL contains query string
推荐答案
只需添加一个检查,看看 nextRoute
不是falsy(即未定义)。我跑这个第一个路由变化时触发。初始路径通常是一个空字符串,这是不通过任何途径引用
Just add a check to see if nextRoute
is not falsy (i.e. undefined). I was running into this when the first route change was triggered. The initial path is usually an empty string, which isn't referenced by any route.
if(nextRoute && nextRoute.auth && !AutenticationService.isLoggedIn()){
$location.path("/login");
}
下面是一个稍微变形例。你可以看到的位置,在控制台改变。
http://plnkr.co/edit/oE2Ew8C68t8k8EgKNqtz?p=$p$pview一>
Here is a slightly modified example. You can see the location changing in the console. http://plnkr.co/edit/oE2Ew8C68t8k8EgKNqtz?p=preview
另一种选择是利用这个项目:
https://github.com/fnakstad/angular-client-side-auth
Another option would be to leverage this project: https://github.com/fnakstad/angular-client-side-auth
下面是一个工作演示:
http://angular-client-side-auth.herokuapp.com/login
Here is a working demo: http://angular-client-side-auth.herokuapp.com/login
这篇关于AngularJS身份验证问题与查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!