I am currently using routeProvider for directing to the different pages, and it works fine with authentication, but needed to add different roles for different users. Most of the answers online point to ui-router, but it would be a hassle to migrate everything to that so is there any way to do this with the basic routeProvider?
Asked
Active
Viewed 88 times
1 Answers
1
This is a small version of what I use in my app:
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/admin', {
templateUrl: 'views/admin.html',
controller: 'AdminCtrl',
roles: ['admin']
});
}).run(function ($rootScope, $location, auth) {
$rootScope.$on('$routeChangeStart', function(e, next) {
if(next.roles && !auth.validRoles(next.roles)) {
e.preventDefault();
$location.path('/error-403');
}
});
});
"auth" is a service where my logged user is stored, and the method validRoles check if the user has the role of the view.

Facundo Pedrazzini
- 1,486
- 14
- 22
-
I am not understanding what you mean by "where my logged user is". If possible, could you set up a plunker or show the service, and the method used. I am new to angular so not that proficient with it. – Anoop May 29 '15 at 22:05
-
Look the HackedByChinese answer http://stackoverflow.com/questions/22537311/angular-ui-router-login-authentication. I inspired on that when I do this. Check the 'principal' services, thats what I talk when i say 'where my logged user is *stored*'. – Facundo Pedrazzini May 31 '15 at 04:10