Currently I'm implementing ng-token-auth
into my Angular application, while this works great, I'm having some trouble with restricting access to certain pages.
In some of my routes I have a couple of extra parameters:
data: {
title: 'Dashboard',
restricted: true, // Only allow logged in users
role: 2 // Only allow a specific role
}
I'm doing this checking login in $stateChangeStart
, so before I switch routes, I can check if the user is allowed to that route.
I followed the ng-token-auth
suggestions about using a parent route with a resolve to check if a user is logged in or not:
resolve: {
auth: function($auth) {
console.log('validate user');
return $auth.validateUser();
}
}
Now the problem comes when I first load up the application, obviously the $stateChangeStart
event is fired before the $auth.validateUser()
has been resolved, because of that the login inside the $stateChangeStart
fails and the user is redirected to the login page.
What would be the better way of implementing this "permissions logic", I don't want to do it per route, as that would add in a lot of extra work and code.
Doing it in the $stateChangeStart
also doesn't seem to be the best options as that doesn't work on first load.