Enabling Authentication on a Collection automatically exposes additional auth-based operations in the Local, REST, and GraphQL APIs.
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:
Example GraphQL Query:
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
.
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:
Example GraphQL Query:
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:
Example GraphQL Mutation:
Example Local API login:
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:
Example GraphQL Mutation:
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:
Example GraphQL Mutation:
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:
Example GraphQL Mutation:
Example Local API verification:
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:
Example GraphQL Mutation:
Example Local API unlock:
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:
Example GraphQL Mutation:
Example Local API forgot 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:
Example GraphQL Mutation: