nextload/src/app/(payload)/access/isAdmin.ts
tobias 8d05864c15
All checks were successful
continuous-integration/drone/push Build is passing
Only check roles if a user when accessing pages
2024-07-06 19:57:55 +02:00

66 lines
1.0 KiB
TypeScript

import { Access } from 'payload/types'
import type { User } from 'types/payload-types'
export const isAdmin = ({ req: { user } }: any) => {
if (!user || !user.roles) {
return false
}
if (user.roles?.includes('admin')) {
return true
}
return false
}
export const isAdminOrCreatedBy = ({ req: { user } }: any) => {
if (!user || !user.roles) {
return false
}
if (user.roles?.includes('admin')) {
return true
}
if (user) {
return {
createdBy: {
equals: user.id,
},
}
}
return false
}
export const isAdminOrSelf = ({ req: { user } }: any) => {
if (!user || !user.roles) {
return false
}
if (user.roles?.includes('admin')) {
return true
}
// Non-admin: can only access themselves
return {
id: {
equals: user.id,
},
}
}
export const isAdminOrPublished = ({ req: { user } }: any) => {
if (user && user.roles) {
if (user.roles?.includes('admin')) {
return true
}
}
return {
_status: {
equals: 'published',
},
}
}