websanova / vue-auth

A simple light-weight authentication library for Vue.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement "administrator" role

nicolasbinet opened this issue · comments

Hi,

I'm looking for a way to implement a "superadmin" role,without having to adding the role to each check in the app.

Is-there a way to either make the "check()" call optional if the user has an "administrator" flag ?
Or a way to extend check() ?

I added a "privilege" method to the prototype, but I can't use it for the vue-router metas

Object.getPrototypeOf(Vue.auth).privilege = (permissions) => {
    if (Vue.auth.user().role === 'administrateur') {
        return true;
    }

    return Vue.auth.check(permissions);
}

thanks for your help.

Nicolas

hmm, sounds a bit strange, did you check the docs on check usage?

You should be able to just do $auth.check('super').

@websanova thanks for the reply.
I know, I could have, but I wanted to skip adding check(['administrateur', ...]) everywhere.

You can just add it to the root route then, did you take a look at this?

https://websanova.com/docs/vue-auth/guides/auth-meta

Yes, and I added meta.auth to the routes also, there I had to add 'administrateur', because I didn't find a way (or don't know how to) to extend the "check" method.

But some interface elements need to be hidden (or shown) depending on the user's role or permissions, and you can't set that through the route.

But I'm not complaining, I was just searching a way to simplify my code.
I don't know, if my explanations are clear enough.

Nicolas

Well, you should be able to override any method after import...

not sure exactly, but something like:

import auth from '@websanova/vue-auth..';

var temp = auth.check

auth.check = function (role) {
    if (!role) {
        role = 'admin'
   }

  return temp(role);
}

Not sure that should be auth.check or auth.prototype.check though, just do some console.log...ing and Googling, should be able to sort it out.

@websanova you made my day !
I forgot about this "temp" trick.
Thanks a lot.

Hi @nicolasbinet, you can attach here example of your solution please? I have same problem.