I develop a website with authentication process. User can be admin or not. I write different components and differents states to route users.
I already control HTTP requests to handle anthentication process, but I want to force redirection when simple user try to go to a not Authorized Page page with typing adress.
Example
He types admin url:
I want to redirect this user to the page where he's coming from, or force him to stay at the current page...
I try to do something like this: www.seanmarchetti.com/authentication_with_angularui_router.html
But I have a different project structure
I control access to states in "admin", and I want to use $state.go
, but to non-admin state..
I tried to use toState
but didn't work (or I probably don't understand how use it properly)
Here is my code from admin.router.js
:
'use strict';
angular.module('appModule.admin')
.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.when('/admin', '/admin/users');
$stateProvider
.state('admin', {
url: '/admin',
redirectTo: 'admin.users',
template: '<admin></admin>',
needAdmin: true,
})
.state('admin.users', {
url: '/users',
template: '<users></users>',
needAdmin: true,
})
.state('admin.apis', {
url: '/apis',
template: '<engines></engines>',
needAdmin: true,
})
})
.run(function ($rootScope, $state, Auth) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
if (toState.needAdmin && !Auth.isAdmin()) {
console.log('fromstate', fromState);
console.log('tostate', toState);
$state.go('/*UNKNOWN STATE FROM HERE*/');
event.preventDefault();
}
});
});
Can anyone help me how can I redirect user properly to .state unknown
from admin
?