Installation
Learn how to install and configure Better Auth in your PayloadCMS project
Installation 🚀
This guide will walk you through installing and configuring Better Auth in your PayloadCMS project.
Prerequisites 📋
- Node.js 18 or later
- PayloadCMS v3 or later
- A running database (all PayloadCMS supported databases)
- Installed required packages, for the moment only better-auth is the only required package
Better Auth Version Compatibility
payload-better-auth@0.8.x
requires better-auth@1.2.7
,
If you have some strange type errors verify both package versions.
Installation 💻
1. Install the package using your preferred package manager:
2. Create the plugin/better-auth config:
By default if you pass an empty object (as const
is important) you will have out-of-the-box the following features:
- PayloadCMS Admin with email and password authentication
- PayloadCMS Admin with Two Factor Authentication (2FA for admins)
- OpenAPI auto documentation at
/api/auth/reference
admin
anduser
Roles and default RBAC (from better-auth's admin plugin)
3. Import and configure the plugin in your PayloadCMS config:
Environment Variables 🔑
Create or update your .env
file:
You can generate a secure secret using:
Best Practices 📚
- Create a single auth layer instance and export it
- Use guards for route protection
- Use checkers for conditional rendering
- Handle errors appropriately
- Cache authentication state when possible