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.

Custom Strategies

Creating a strategy

At the core, a strategy is a way to authenticate a user making a request. As of 3.0 we moved away from Passport in favor of pulling back the curtain and putting you in full control.

A strategy is made up of the following:

Parameter

Description

name \*

The name of your strategy

authenticate \*

A function that takes in the parameters below and returns a user or null.

The authenticate function is passed the following arguments:

Argument

Description

headers \*

The headers on the incoming request. Useful for retrieving identifiable information on a request.

payload \*

The Payload class. Useful for authenticating the identifiable information against Payload.

isGraphQL

Whether or not the request was made from a GraphQL endpoint. Default is false.

Example Strategy

At its core a strategy simply takes information from the incoming request and returns a user. This is exactly how Payload's built-in strategies function.

Your authenticate method should return an object containing a Payload user document and any optional headers that you'd like Payload to set for you when we return a response.

1
import type { CollectionConfig } from 'payload'
2
export const Users: CollectionConfig = {
3
slug: 'users',
4
auth: {
5
disableLocalStrategy: true,
6
strategies: [
7
{
8
name: 'custom-strategy',
9
authenticate: ({ payload, headers }) => {
10
const usersQuery = await payload.find({
11
collection: 'users',
12
where: {
13
code: {
14
equals: headers.get('code'),
15
},
16
secret: {
17
equals: headers.get('secret'),
18
},
19
},
20
})
21
return {
22
// Send the user back to authenticate,
23
// or send null if no user should be authenticated
24
user: usersQuery.docs[0] || null,
25
// Optionally, you can return headers
26
// that you'd like Payload to set here when
27
// it returns the response
28
responseHeaders: new Headers({
29
'some-header': 'my header value'
30
})
31
}
32
}
33
}
34
]
35
},
36
fields: [
37
{
38
name: 'code',
39
type: 'text',
40
index: true,
41
unique: true,
42
},
43
{
44
name: 'secret',
45
type: 'text',
46
},
47
]
48
}
Next

Token Data