### What? I noticed a spelling error in the banner of the beta docs and decided I could save everyone some time by *running the entirety of the beta docs* through a spellchecker. ### Why? To fix many spelling and formatting mistakes at once. ### How? By enabling `edit mode` in my browser and letting the built-in spellchecker perform its magic (and changing _only_ where it made sense). ~~Ironically, the original spelling mistake that inspired me to do this remains unchanged as that is a part of the website repo. [PR for that is here](https://github.com/payloadcms/website/pull/388).~~
108 lines
2.5 KiB
Plaintext
108 lines
2.5 KiB
Plaintext
---
|
|
title: Token Data
|
|
label: Token Data
|
|
order: 70
|
|
desc: Storing data for read on the request object.
|
|
keywords: authentication, config, configuration, documentation, Content Management System, cms, headless, javascript, node, react, nextjs
|
|
---
|
|
|
|
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.
|
|
|
|
```ts
|
|
import type { CollectionConfig } from 'payload'
|
|
|
|
export const Users: CollectionConfig = {
|
|
slug: 'users',
|
|
auth: true,
|
|
fields: [
|
|
{
|
|
// will be stored in the JWT
|
|
saveToJWT: true,
|
|
type: 'select',
|
|
name: 'role',
|
|
options: [
|
|
'super-admin',
|
|
'user',
|
|
]
|
|
},
|
|
{
|
|
// the entire object will be stored in the JWT
|
|
// tab fields can do the same thing!
|
|
saveToJWT: true,
|
|
type: 'group',
|
|
name: 'group1',
|
|
fields: [
|
|
{
|
|
type: 'text',
|
|
name: 'includeField',
|
|
},
|
|
{
|
|
// will be omitted from the JWT
|
|
saveToJWT: false,
|
|
type: 'text',
|
|
name: 'omitField',
|
|
},
|
|
]
|
|
},
|
|
{
|
|
type: 'group',
|
|
name: 'group2',
|
|
fields: [
|
|
{
|
|
// will be stored in the JWT
|
|
// but stored at the top level
|
|
saveToJWT: true,
|
|
type: 'text',
|
|
name: 'includeField',
|
|
},
|
|
{
|
|
type: 'text',
|
|
name: 'omitField',
|
|
},
|
|
]
|
|
},
|
|
]
|
|
}
|
|
```
|
|
|
|
<Banner type="success">
|
|
<strong>Tip:</strong>
|
|
<br/>
|
|
If you wish to use a different key other than the field `name`, you can define `saveToJWT` as a string.
|
|
</Banner>
|
|
|
|
|
|
### Using Token Data
|
|
|
|
This is especially helpful when writing [Hooks](../hooks/overview) and [Access Control](../access-control/overview) that depend on user defined fields.
|
|
|
|
```ts
|
|
import type { CollectionConfig } from 'payload'
|
|
|
|
export const Invoices: CollectionConfig = {
|
|
slug: 'invoices',
|
|
access: {
|
|
read: ({ req, data }) => {
|
|
if (!req?.user) return false
|
|
// highlight-start
|
|
if ({ req.user?.role === 'super-admin'}) {
|
|
return true
|
|
}
|
|
// highlight-end
|
|
return data.owner === req.user.id
|
|
}
|
|
}
|
|
fields: [
|
|
{
|
|
name: 'owner',
|
|
relationTo: 'users'
|
|
},
|
|
// ... other fields
|
|
],
|
|
}
|
|
```
|