Auth Example for Payload CMS
This example demonstrates how to implement Authentication in Payload CMS.
There is a fully working Next.js app tailored specifically for this example which can be found here. Follow the instructions there to get started. If you are setting up authentication for another front-end, please consider contributing to this repo with your own example!
Getting Started
- Clone this repo
cdinto this directory and runyarnornpm installcp .env.example .envto copy the example environment variablesyarn devornpm run devto start the server and seed the databaseopen http://localhost:8000/adminto access the admin panel- Login with email
dev@payloadcms.comand passwordtest
How it works
An auth-enabled users collection is create which opens all auth-related operations needed to create a fully custom workflow on your front-end using the REST or GraphQL APIs, including:
MeLoginLogoutRefresh TokenVerify EmailUnlockForgot PasswordReset Password
The cors, csrf, and cookies settings are also configured to ensure that the admin panel and front-end can communicate with each other securely.
Role-based Access Control
Basic role-based access control is setup to determine what users can and cannot do based on their roles, which are:
admin: They can access the Payload admin panel to manage your application. They can see all data and make all operations.user: They cannot access the Payload admin panel and have a limited access to operations based on their user.
A beforeChange field hook called protectRoles is placed on this to automatically populate roles with the user role when a new user is created. It also protects roles from being changed by non-admins.
Seed
On boot, a seed script is included to create a user with the role admin.