Authentication
The SaaS Boilerplate uses Better Auth for handling authentication. Better Auth interacts directly with your PostgreSQL database to store user sessions and account information.
How it works
- Client: The frontend uses
better-auth/clientto initiate sign-in requests (e.g., "Sign in with Google"). - Server: The backend verifies the request and communicates with the provider (Google, GitHub, LinkedIn).
- Database: Upon successful authentication, a user record is created (if new) and a session is stored in the database.
- Session: A secure, HTTP-only cookie is set on the user's browser to maintain the session.
Configuration
All authentication logic is located in packages/auth.
- Providers: Configured in
packages/auth/src/auth.ts. - Environment Variables: You must provide Client IDs and Secrets in your
.envfile for each provider you wish to enable.
Adding a New Provider
To add a new provider (e.g., Discord):
- Install the provider package/plugin if required.
- Update
packages/auth/src/auth.tsto include the new provider in theprovidersarray. - Add the necessary environment variables.
