inca / voie

[UNMAINTAINED] Simple Vue.js router / layout manager
141 stars 7 forks source link

Global hooks #19

Closed jonagoldman closed 8 years ago

jonagoldman commented 8 years ago

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.

inca commented 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?

jonagoldman commented 8 years ago

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?

Teknologica commented 8 years ago

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
});
inca commented 8 years ago

Hey, beforeEach and afterEach are available in 0.7.0, let me know if they work as you expect them to ;)

jonagoldman commented 8 years ago

haha great! will try it in the next few days.

jonagoldman commented 8 years ago

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?