chore: fixes auth example protectRoles function

This commit is contained in:
Jarrod Flesch
2023-07-07 11:34:19 -04:00
parent cfeeb9758f
commit 5b7756e266
3 changed files with 14 additions and 7 deletions

View File

@@ -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

View File

@@ -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]
}

View File

@@ -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": [