Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Token Data

During the lifecycle of a request you will be able to access the data you have configured to be stored in the JWT by accessing req.user. The user object is automatically appended to the request for you.

Definining Token Data

You can specify what data gets encoded to the Cookie/JWT-Token by setting saveToJWT property on fields within your auth collection.

1
import type { CollectionConfig } from 'payload'
2
3
export const Users: CollectionConfig = {
4
slug: 'users',
5
auth: true,
6
fields: [
7
{
8
// will be stored in the JWT
9
saveToJWT: true,
10
type: 'select',
11
name: 'role',
12
options: [
13
'super-admin',
14
'user',
15
]
16
},
17
{
18
// the entire object will be stored in the JWT
19
// tab fields can do the same thing!
20
saveToJWT: true,
21
type: 'group',
22
name: 'group1',
23
fields: [
24
{
25
type: 'text',
26
name: 'includeField',
27
},
28
{
29
// will be omitted from the JWT
30
saveToJWT: false,
31
type: 'text',
32
name: 'omitField',
33
},
34
]
35
},
36
{
37
type: 'group',
38
name: 'group2',
39
fields: [
40
{
41
// will be stored in the JWT
42
// but stored at the top level
43
saveToJWT: true,
44
type: 'text',
45
name: 'includeField',
46
},
47
{
48
type: 'text',
49
name: 'omitField',
50
},
51
]
52
},
53
]
54
}

Using Token Data

This is especially helpful when writing Hooks and Access Control that depend on user defined fields.

1
import type { CollectionConfig } from 'payload'
2
3
export const Invoices: CollectionConfig = {
4
slug: 'invoices',
5
access: {
6
read: ({ req, data }) => {
7
if (!req?.user) return false
8
if ({ req.user?.role === 'super-admin'}) {
9
return true
10
}
11
return data.owner === req.user.id
12
}
13
}
14
fields: [
15
{
16
name: 'owner',
17
relationTo: 'users'
18
},
19
// ... other fields
20
],
21
}
Next

Overview