#Prevent users from accessing admin panel

16 messages · Page 1 of 1 (latest)

sharp olive
#

How do i define which loged in users can access the admin, and which cannot?

Do I need two seperate user collections (one for admin, one for regular user), or can I add a flag to my user collection for indicating admins?

dire bison
sharp olive
#

@dire bison this makes me think that i need two seperate collections for users and for admins, is that correct?

#

(as in, i can "assign" one collection to be the admin collection, and I cannot make this assignment on a per-user level)

dire bison
#

You could still use a single collection and implement role or user-based access control

#

Or go even simpler and just add a checkbox to the user and check that

sharp olive
#

@dire bison that is super helpful. thansk a bunch!

sharp olive
#

sorry @dire bison - one more question regarding this. How do i limit the access to the admin pabel based on a checkbox like that?

#

as in, I dont want normal users being able to login in /admin

dire bison
#

Add a canAccessAdmin checkbox field to the user collection and then in your access.admin hook, do something like this:

export const admins: Access = ({ req: { user } }) => user.canAccessAdmin
sharp olive
#

makes sense, but what i dont quite understand is where to use the const Admins of your example after I defined it.

dire bison
#

On your user collection, like this:

{
  slug: 'users',
  auth: true,
  access: {
    admin: admins
  }
}
sharp olive
#

ahhh

#

i really missed the obvious here, didnt I

#

thank you so much!