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