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.
Defining Token DataYou 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'
3 export const Users : CollectionConfig = {
12 options : [ 'super-admin' , 'user' ] ,
Tip:
If you wish to use a different key other than the field name, you can define saveToJWT as a string.
Using Token DataThis is especially helpful when writing Hooks and Access Control that depend on user defined fields.
1 import type { CollectionConfig } from 'payload'
3 export const Invoices : CollectionConfig = {
6 read : ( { req , data } ) => {
7 if ( ! req ?. user ) return false
8 if ( { req . user ?. role === 'super-admin' } ) {
11 return data . owner === req . user . id