Javascript Angularjs登录验证:防止用户导航到除登录页面和注册页面之外的其他页面
我正在为我的前端使用AngularJS框架开发一个web应用程序。对于我的登录页面,我必须阻止用户浏览到除登录页面和注册之外的其他页面。但我现在所做的代码也阻止用户导航到注册页面。下面是我的代码。我如何才能解决这个问题,以使用户能够浏览到登录页面和注册页面,只有当用户没有登录Javascript Angularjs登录验证:防止用户导航到除登录页面和注册页面之外的其他页面,javascript,angularjs,authentication,basic-authentication,Javascript,Angularjs,Authentication,Basic Authentication,我正在为我的前端使用AngularJS框架开发一个web应用程序。对于我的登录页面,我必须阻止用户浏览到除登录页面和注册之外的其他页面。但我现在所做的代码也阻止用户导航到注册页面。下面是我的代码。我如何才能解决这个问题,以使用户能够浏览到登录页面和注册页面,只有当用户没有登录 .run(function ($rootScope, $state, AuthService, AUTH_EVENTS) { $rootScope.$on('$stateChangeStart', function (
.run(function ($rootScope, $state, AuthService, AUTH_EVENTS) {
$rootScope.$on('$stateChangeStart', function (event,next, nextParams, fromState) {
if ('data' in next && 'authorizedRoles' in next.data) {
var authorizedRoles = next.data.authorizedRoles;
if (!AuthService.isAuthorized(authorizedRoles)) {
event.preventDefault();
$state.go($state.current, {}, {reload: true});
$rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
}
}
if (!AuthService.isAuthenticated()) {
if (next.name !== 'login') {
event.preventDefault();
$state.go('login');
}
}
});
您可以通过在.state的数据属性中添加一个布尔参数来实现这一点,比如说requireAuth,并在.run块中检查它 下面是这方面的伪代码 in.config块 然后进去,跑街区
提示:不要将next命名为参数,因为它们是节点中的关键字,可能会conflicts@pro.mean必须将next作为参数。如果不是,则应用程序不工作。我的意思是只更改参数名称。看到下面的答案,我说这是伪代码,你必须根据需要修改
$stateProvider
.state("register", {
url: '/register',
templateUrl: 'register.html',
controller:'UserController',
controllerAs: 'vm',
data: {
requiresAuth: false,
pageTitle: 'Register'
}
})
.state("dashboard", {
url: '/dashboard',
templateUrl: 'dashboard.html',
controller:'OtherController',
controllerAs: 'vm',
data: {
requiresAuth: true,
pageTitle: 'Dashboard',
authorizedRoles: ['WHATEVER_ROLE']
}
});
var stateChangeStart = $rootScope.$on('$stateChangeStart', function(event, toState, toParams) {
if (AuthService.isAuthenticated()) {
// if user trying to access register/forgot page after login than redirect to dashboard
if (!toState.data.requiresAuth) {
event.preventDefault();
$rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
}
// user is not authenticated and trying to access page which is not permissible than send back to dashboard
if (angular.isDefined(toState.data.authorizedRoles)) {
var roles = toState.data.authorizedRoles;
AuthService.isAuthorized(roles).catch(function() { // NOTE: here we are only handling with .catch block
event.preventDefault();
$rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
});
}
}
// user is not authenticated than redirect to login
else if (toState.data.requiresAuth) {
event.preventDefault();
$rootScope.$broadcast(AUTH_EVENTS.notAuthenticated);
}
});
var notAuthenticated = $rootScope.$on(AUTH_EVENTS.notAuthenticated, function() {
$log.warn('not authenticated');
$state.go('login', null, {});
return;
});
var notAuthorized = $rootScope.$on(AUTH_EVENTS.notAuthorized, function() {
$log.warn('not authorized');
$state.go('dashboard');
return;
});
// DO NOT forget to destroy
$rootScope.$on('$destroy', notAuthenticated);
$rootScope.$on('$destroy', notAuthorized);
$rootScope.$on('$destroy', stateChangeStart);