[英]how to restrict logged user to move on login page or signup page in angular js?
i have following code : 我有以下代码:
.run(function($rootScope, $location, Auth) {
//Redirect to login if route requires auth and you're not logged in
$rootScope.$on('$routeChangeStart', function(event, next) {
Auth.isLoggedInAsync(function(loggedIn) {
if (!loggedIn) {//when user is not logged in
if ($location.path() == "/participant/signup" || $location.path() == "/researcher/signup" || $location.path() == "/participant/login" || $location.path() == "/researcher/login" || $location.path() == "/admin/login");
else{
$location.path('/homePage');
}
}
else if(loggedIn){ //when user loged in
if ($location.path() == "/participant/signup" || $location.path() == "/researcher/signup" || $location.path() == "/participant/login" || $location.path() == "/researcher/login" || $location.path() == "/admin/login"){
event.preventDefault();
//event.stopPropagation();
}
}
});
});
})
i want to restrict logged user to move on login or signup page. 我想限制登录用户在登录或注册页面上移动。 the above code is not working please suggest me where i am doing wrong.
上面的代码不起作用,请建议我在哪里做错了。 if you have a good way to handle this please suggest?
如果您有解决此问题的好方法,请提出建议?
Define an interceptor to handle user status
angular.module('App').factory('authInterceptor', ['$location', 'Auth', function ($location, Auth) {
var authInterceptorService = {};
if (!Auth.isLoggedIn)
$location.url('/login');
return authInterceptorService;
}]);
In App.js 在App.js中
angular.module('App').config(function ($httpProvider) {
$httpProvider.interceptors.push('authInterceptorService');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.