Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling multiple roles from different keys #697

Open
jongbonga opened this issue Apr 6, 2023 · 2 comments
Open

Handling multiple roles from different keys #697

jongbonga opened this issue Apr 6, 2023 · 2 comments

Comments

@jongbonga
Copy link

Hey

I have this structure

user1 = {
    name: 'John',
    department: ['tech', 'operations'],
    tech: { role: 'manager' }
}


user2 = {
    name: 'Steve',
    department: ['hr'],
    hr: { role: 'manager' }
}

My rolesKey is set to department.

I want to allow all to log in regardless of their departments but go to /unauthorized if they don't have the department tech. if they do have it, they will still be unauthorized if they don't have the role of manager or admin

my options look like this

options: {
    rolesKey: "department",
    notFoundRedirect: { path: "/dashboard" },
    forbiddenRedirect: { path: "/unauthorized" }
}

my router meta looks like this

meta: {
  roles: ["manager", "admin"],
  rolesKey: "tech.role"
}

my user2 is logged in but not being redirected anywhere, not even to the forbuddenRedirect.
Is there a way of handling this use case? how do I avoid the error properties of undefined (reading 'role') when user2 isn't part of tech and thus can't access the tech.role?

@websanova
Copy link
Owner

You may just be missing the auth param in your meta object, take a look at the demo routes here:

https://github.com/websanova/vue-auth/blob/master/demos/2.x/src/router/index.js#L54

@jongbonga
Copy link
Author

I've implemented it successfully on different projects. But I get an error when the roleskey tech.role is not available on user object. Some users do have, some don't. Is there a where to check is a key exist for the roleskey?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants