#Prevent users from accessing admin panel
16 messages · Page 1 of 1 (latest)
Would access.admin work for you? https://payloadcms.com/docs/access-control/collections
@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)
You could still use a single collection and implement role or user-based access control
You can either assign a roles select field on the user and check that in your access control function, here's example of that https://github.com/payloadcms/template-ecommerce/blob/main/src/access/admins.ts
Or go even simpler and just add a checkbox to the user and check that
@dire bison that is super helpful. thansk a bunch!
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
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
makes sense, but what i dont quite understand is where to use the const Admins of your example after I defined it.
On your user collection, like this:
{
slug: 'users',
auth: true,
access: {
admin: admins
}
}