Closed jonagoldman closed 8 years ago
So you're looking for something like beforeEach
with access to state-specific contexts? I assume the global ancestor state like authenticated
from which all protected states inherit doesn't satisfy your needs?
Yes, beforeEach
or similar.
Because this is how my app looks now:
// 'settings' state
enter: (ctx) => {
if ( ! permissionService.userCan('access_settings')) {
return { redirect: 'app' };
}
}
// 'billing' state
enter: (ctx) => {
if ( ! permissionService.userCan('access_billing')) {
return { redirect: 'app' };
}
}
// and so on...
A global hook can simplify this, maybe... What do you think?
I had to do something similar. I created a function that wraps around the StateManager add
to handle permissions automatically. It also wraps around any other enter
delegates that get defined and daisy chains them.
Router.add({
name: 'dashboard',
path: '/dashboard',
parent: 'main',
checkPermissions: false, // adds a predefined enter permission check if true
component: DashboardView
});
Hey, beforeEach
and afterEach
are available in 0.7.0, let me know if they work as you expect them to ;)
haha great! will try it in the next few days.
Seems to work perfectly, thanks! By the way, I see the 0.7.0 release in npm but not in GitHub releases. Are you using master as latest release?
I'm developing an app where I need to check if the user has permissions to access each state. Right now I'm doing the check in each 'enter' hook. Would be useful to have a global hook / middleware so I can just check in one place.