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.

Authentication Operations

Enabling Authentication on a Collection automatically exposes additional auth-based operations in the Local, REST, and GraphQL APIs.

Access

The Access operation returns what a logged in user can and can't do with the collections and globals that are registered via your config. This data can be immensely helpful if your app needs to show and hide certain features based on access control, as the Payload Admin panel does.

REST API endpoint:

GET http://localhost:3000/api/access

Example response:

1
{
2
canAccessAdmin: true,
3
collections: {
4
pages: {
5
create: {
6
permission: true,
7
},
8
read: {
9
permission: true,
10
},
11
update: {
12
permission: true,
13
},
14
delete: {
15
permission: true,
16
},
17
fields: {
18
title: {
19
create: {
20
permission: true,
21
},
22
read: {
23
permission: true,
24
},
25
update: {
26
permission: true,
27
},
28
}
29
}
30
}
31
}
32
}

Example GraphQL Query:

1
query {
2
Access {
3
pages {
4
read {
5
permission
6
}
7
}
8
}
9
}

Document access can also be queried on a collection/global basis. Access on a global can queried like http://localhost:3000/api/global-slug/access, Collection document access can be queried like http://localhost:3000/api/collection-slug/access/:id.

Me

Returns either a logged in user with token or null when there is no logged in user.

REST API endpoint:

GET http://localhost:3000/api/[collection-slug]/me

Example response:

1
{
2
user: { // The JWT "payload" ;) from the logged in user
3
email: 'dev@payloadcms.com',
4
createdAt: "2020-12-27T21:16:45.645Z",
5
updatedAt: "2021-01-02T18:37:41.588Z",
6
id: "5ae8f9bde69e394e717c8832"
7
},
8
token: '34o4345324...', // The token that can be used to authenticate the user
9
exp: 1609619861, // Unix timestamp representing when the user's token will expire
10
}

Example GraphQL Query:

1
query {
2
me[collection-singular-label] {
3
user {
4
email
5
}
6
exp
7
}
8
}

Login

Accepts an email and password. On success, it will return the logged in user as well as a token that can be used to authenticate. In the GraphQL and REST APIs, this operation also automatically sets an HTTP-only cookie including the user's token. If you pass an Express res to the Local API operation, Payload will set a cookie there as well.

Example REST API login:

1
const res = await fetch('http://localhost:3000/api/[collection-slug]/login', {
2
method: 'POST',
3
headers: {
4
'Content-Type': 'application/json',
5
},
6
body: JSON.stringify({
7
email: 'dev@payloadcms.com',
8
password: 'this-is-not-our-password...or-is-it?',
9
}),
10
})
11
12
const json = await res.json()
13
14
// JSON will be equal to the following:
15
/*
16
{
17
user: {
18
email: 'dev@payloadcms.com',
19
createdAt: "2020-12-27T21:16:45.645Z",
20
updatedAt: "2021-01-02T18:37:41.588Z",
21
id: "5ae8f9bde69e394e717c8832"
22
},
23
token: '34o4345324...',
24
exp: 1609619861
25
}
26
*/

Example GraphQL Mutation:

1
mutation {
2
login[collection-singular-label](email: "dev@payloadcms.com", password: "yikes") {
3
user {
4
email
5
}
6
exp
7
token
8
}
9
}

Example Local API login:

1
const result = await payload.login({
2
collection: '[collection-slug]',
3
data: {
4
email: 'dev@payloadcms.com',
5
password: 'get-out',
6
},
7
})

Logout

As Payload sets HTTP-only cookies, logging out cannot be done by just removing a cookie in JavaScript, as HTTP-only cookies are inaccessible by JS within the browser. So, Payload exposes a logout operation to delete the token in a safe way.

Example REST API logout:

1
const res = await fetch('http://localhost:3000/api/[collection-slug]/logout', {
2
method: 'POST',
3
headers: {
4
'Content-Type': 'application/json',
5
},
6
})

Example GraphQL Mutation:

1
mutation {
2
logout[collection-singular-label]
3
}

Refresh

Allows for "refreshing" JWTs. If your user has a token that is about to expire, but the user is still active and using the app, you might want to use the refresh operation to receive a new token by executing this operation via the authenticated user.

This operation requires a non-expired token to send back a new one. If the user's token has already expired, you will need to allow them to log in again to retrieve a new token.

If successful, this operation will automatically renew the user's HTTP-only cookie and will send back the updated token in JSON.

Example REST API token refresh:

1
const res = await fetch('http://localhost:3000/api/[collection-slug]/refresh-token', {
2
method: 'POST',
3
headers: {
4
'Content-Type': 'application/json',
5
},
6
})
7
8
const json = await res.json()
9
10
// JSON will be equal to the following:
11
/*
12
{
13
user: {
14
email: 'dev@payloadcms.com',
15
createdAt: "2020-12-27T21:16:45.645Z",
16
updatedAt: "2021-01-02T18:37:41.588Z",
17
id: "5ae8f9bde69e394e717c8832"
18
},
19
refreshedToken: '34o4345324...',
20
exp: 1609619861
21
}
22
*/

Example GraphQL Mutation:

1
mutation {
2
refreshToken[collection-singular-label] {
3
user {
4
email
5
}
6
refreshedToken
7
}
8
}

Verify by Email

If your collection supports email verification, the Verify operation will be exposed which accepts a verification token and sets the user's _verified property to true, thereby allowing the user to authenticate with the Payload API.

Example REST API user verification:

1
const res = await fetch(`http://localhost:3000/api/[collection-slug]/verify/${TOKEN_HERE}`, {
2
method: 'POST',
3
headers: {
4
'Content-Type': 'application/json',
5
},
6
})

Example GraphQL Mutation:

1
mutation {
2
verifyEmail[collection-singular-label](token: "TOKEN_HERE")
3
}

Example Local API verification:

1
const result = await payload.verifyEmail({
2
collection: '[collection-slug]',
3
token: 'TOKEN_HERE',
4
})

Unlock

If a user locks themselves out and you wish to deliberately unlock them, you can utilize the Unlock operation. The Admin panel features an Unlock control automatically for all collections that feature max login attempts, but you can programmatically unlock users as well by using the Unlock operation.

To restrict who is allowed to unlock users, you can utilize the unlock access control function.

Example REST API unlock:

1
const res = await fetch(`http://localhost:3000/api/[collection-slug]/unlock`, {
2
method: 'POST',
3
headers: {
4
'Content-Type': 'application/json',
5
},
6
body: JSON.stringify({
7
email: 'dev@payloadcms.com',
8
}),
9
})

Example GraphQL Mutation:

1
mutation {
2
unlock[collection-singular-label](email: "dev@payloadcms.com")
3
}

Example Local API unlock:

1
const result = await payload.unlock({
2
collection: '[collection-slug]',
3
data: {
4
email: 'dev@payloadcms.com',
5
},
6
})

Forgot Password

Payload comes with built-in forgot password functionality. Submitting an email address to the Forgot Password operation will generate an email and send it to the respective email address with a link to reset their password.

The link to reset the user's password contains a token which is what allows the user to securely reset their password.

By default, the Forgot Password operations send users to the Payload Admin panel to reset their password, but you can customize the generated email to send users to the frontend of your app instead by overriding the email HTML.

Example REST API Forgot Password:

1
const res = await fetch(`http://localhost:3000/api/[collection-slug]/forgot-password`, {
2
method: 'POST',
3
headers: {
4
'Content-Type': 'application/json',
5
},
6
body: JSON.stringify({
7
email: 'dev@payloadcms.com',
8
}),
9
})

Example GraphQL Mutation:

1
mutation {
2
forgotPassword[collection-singular-label](email: "dev@payloadcms.com")
3
}

Example Local API forgot password:

1
const token = await payload.forgotPassword({
2
collection: '[collection-slug]',
3
data: {
4
email: 'dev@payloadcms.com',
5
},
6
disableEmail: false, // you can disable the auto-generation of email via local API
7
})

Reset Password

After a user has "forgotten" their password and a token is generated, that token can be used to send to the reset password operation along with a new password which will allow the user to reset their password securely.

Example REST API Reset Password:

1
const res = await fetch(`http://localhost:3000/api/[collection-slug]/reset-password`, {
2
method: 'POST',
3
headers: {
4
'Content-Type': 'application/json',
5
},
6
body: JSON.stringify({
7
token: 'TOKEN_GOES_HERE'
8
password: 'not-today',
9
}),
10
});
11
12
const json = await res.json();
13
14
// JSON will be equal to the following:
15
/*
16
{
17
user: {
18
email: 'dev@payloadcms.com',
19
createdAt: "2020-12-27T21:16:45.645Z",
20
updatedAt: "2021-01-02T18:37:41.588Z",
21
id: "5ae8f9bde69e394e717c8832"
22
},
23
token: '34o4345324...',
24
exp: 1609619861
25
}
26
*/

Example GraphQL Mutation:

1
mutation {
2
resetPassword[collection-singular-label](token: "TOKEN_GOES_HERE", password: "not-today")
3
}
Next

Using the Payload Auth Middleware