Skip to content

Commit

Permalink
docs: send user back with the correct type
Browse files Browse the repository at this point in the history
  • Loading branch information
namick committed Dec 28, 2024
1 parent 7a4d53a commit 62954d3
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions docs/authentication/custom-strategies.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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. |
| **`authenticate`** \* | A function that takes in the parameters below and returns a user or null. |

The `authenticate` function is passed the following arguments:

Expand All @@ -38,7 +38,7 @@ At its core a strategy simply takes information from the incoming request and re
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.

```ts
import type { CollectionConfig } from 'payload'
import type { CollectionConfig, User } from 'payload'

export const Users: CollectionConfig = {
slug: 'users',
Expand All @@ -48,7 +48,7 @@ export const Users: CollectionConfig = {
strategies: [
{
name: 'custom-strategy',
authenticate: ({ payload, headers }) => {
authenticate: async ({ payload, headers }) => {
const usersQuery = await payload.find({
collection: 'users',
where: {
Expand All @@ -61,10 +61,19 @@ export const Users: CollectionConfig = {
},
})

// Send null if no user should be authenticated
if (!usersQuery.docs[0]) return { user: null }

const user: User = {
...usersQuery.docs[0],

// Returned user also needs the collection property
collection: 'users',
}

return {
// Send the user back to authenticate,
// or send null if no user should be authenticated
user: usersQuery.docs[0] || null,
// Send the user back to authenticate
user,

// Optionally, you can return headers
// that you'd like Payload to set here when
Expand Down

0 comments on commit 62954d3

Please sign in to comment.