Maybe I'm doing something wrong, but I can't seem to get the access restrictions to work. I copied over this code from v2 (slightly modified):
{
access: {
admin: ({ req: { user } }) => user?.role === "super",
create: () => false,
delete: () => false,
},
// ...
}
I get an error saying that
req
is undefined. No idea how the callback is being called and with what arguments. Is this a bug? Let me know if you need more info.
Hmm
If you do like
admin: (data) =>
and log data, what is the value/
Curious to see if the req shape is different
I get the following, with some info redacted:
{
payload: <ref *1> BasePayload {
authStrategies: [ [Object] ],
collections: {
users: [Object],
'payload-preferences': [Object],
'payload-migrations': [Object]
},
config: {
...
},
create: [AsyncFunction: create],
db: {
...
},
decrypt: [Function: decrypt],
duplicate: [AsyncFunction: duplicate],
email: Promise {
...
},
emailOptions: { fromAddress: 'info@payloadcms.com', fromName: 'Payload' },
encrypt: [Function: encrypt],
extensions: undefined,
find: [AsyncFunction: find],
findByID: [AsyncFunction: findByID],
findGlobal: [AsyncFunction: findGlobal],
findGlobalVersionByID: [AsyncFunction: findGlobalVersionByID],
findGlobalVersions: [AsyncFunction: findGlobalVersions],
findVersionByID: [AsyncFunction: findVersionByID],
findVersions: [AsyncFunction: findVersions],
forgotPassword: [AsyncFunction: forgotPassword],
getAPIURL: [Function: getAPIURL],
getAdminURL: [Function: getAdminURL],
globals: { config: [] },
logger: EventEmitter {
...
},
login: [AsyncFunction: login],
resetPassword: [AsyncFunction: resetPassword],
restoreGlobalVersion: [AsyncFunction: restoreGlobalVersion],
restoreVersion: [AsyncFunction: restoreVersion],
schema: undefined,
secret: 'dd4cf5ac6a674a17948ce72497c8e562',
sendEmail: [Function: bound sendEmail] AsyncFunction,
types: undefined,
unlock: [AsyncFunction: unlock],
updateGlobal: [AsyncFunction: updateGlobal],
validationRules: undefined,
verifyEmail: [AsyncFunction: verifyEmail],
versions: {}
},
user: {
id: 1,
name: null,
role: 'super',
updatedAt: '2024-04-02T15:12:41.962Z',
createdAt: '2024-04-02T15:12:41.962Z',
email: '...',
loginAttempts: 0,
collection: 'users',
_strategy: 'local-jwt'
}
}
If you need the full output, let me know. I thought this amount would be enough to get an idea of the structure.
ah so that's your
req
object
because it has req.payload
req.user
etc
access: {
admin: (req) => req.user?.role === "super",
create: () => false,
delete: () => false,
},
I'm guessing that works, without the destruction
or uh...destructuring?
Was this a change from v2.0 to v3.0?
And that did do the trick, thanks.
Star
Discord
online
Get dedicated engineering support directly from the Payload team.