chore: fixes auth example protectRoles function
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
PAYLOAD_PUBLIC_SITE_URL=http://localhost:3001
|
PAYLOAD_PUBLIC_SITE_URL=http://localhost:3001
|
||||||
PAYLOAD_PUBLIC_SERVER_URL=http://localhost:3000
|
PAYLOAD_PUBLIC_SERVER_URL=http://localhost:3000
|
||||||
MONGODB_URI=mongodb://localhost/payload-example-auth
|
MONGODB_URI=mongodb://127.0.0.1/payload-example-auth
|
||||||
PAYLOAD_SECRET=PAYLOAD_AUTH_EXAMPLE_SECRET_KEY
|
PAYLOAD_SECRET=PAYLOAD_AUTH_EXAMPLE_SECRET_KEY
|
||||||
COOKIE_DOMAIN=localhost
|
COOKIE_DOMAIN=localhost
|
||||||
PAYLOAD_SEED=true
|
PAYLOAD_SEED=true
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import type { FieldHook } from 'payload/types'
|
|||||||
|
|
||||||
// ensure there is always a `user` role
|
// ensure there is always a `user` role
|
||||||
// do not let non-admins change roles
|
// do not let non-admins change roles
|
||||||
// eslint-disable-next-line consistent-return
|
|
||||||
export const protectRoles: FieldHook<User & { id: string }> = async ({ req, data }) => {
|
export const protectRoles: FieldHook<User & { id: string }> = async ({ req, data }) => {
|
||||||
const isAdmin = req.user?.roles.includes('admin') || data.email === 'demo@payloadcms.com' // for the seed script
|
const isAdmin = req.user?.roles.includes('admin') || data.email === 'demo@payloadcms.com' // for the seed script
|
||||||
|
|
||||||
@@ -11,5 +10,8 @@ export const protectRoles: FieldHook<User & { id: string }> = async ({ req, data
|
|||||||
return ['user']
|
return ['user']
|
||||||
}
|
}
|
||||||
|
|
||||||
return [...(data?.roles || []), 'user']
|
const userRoles = new Set(data?.roles || [])
|
||||||
|
userRoles.add('user')
|
||||||
|
|
||||||
|
return [...userRoles]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es5",
|
"target": "es2019",
|
||||||
"lib": [
|
"lib": [
|
||||||
"dom",
|
"dom",
|
||||||
"dom.iterable",
|
"dom.iterable",
|
||||||
@@ -14,10 +14,15 @@
|
|||||||
"rootDir": "./src",
|
"rootDir": "./src",
|
||||||
"jsx": "react",
|
"jsx": "react",
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"paths": {
|
"paths": {
|
||||||
"payload/generated-types": ["./src/payload-types.ts"],
|
"payload/generated-types": [
|
||||||
"node_modules/*": ["./node_modules/*"]
|
"./src/payload-types.ts"
|
||||||
|
],
|
||||||
|
"node_modules/*": [
|
||||||
|
"./node_modules/*"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
|
|||||||
Reference in New Issue
Block a user