Authentication
Learn how authentication works in Dirstarter, including BetterAuth setup, providers, user management, and security
Dirstarter uses BetterAuth for authentication, providing a flexible and secure system with multiple authentication methods and role-based access control.
Core Features
- Magic Link authentication with Resend integration
- Social login providers (Google, GitHub)
- Role-based access control (admin/user)
- Session management
- User management interface
Setup
The following environment variables are required:
You can generate a random string for the secret key using openssl rand -base64 32
.
For every OAuth provider you want to use, you need to set the clientId
and clientSecret
provider variables.
Follow the BetterAuth documentation for more information where to find the clientId and clientSecret.
Basic Configuration
Authentication is configured in lib/auth.ts
:
For a full list of available configuration options, plugins and providers see the BetterAuth documentation.
Authentication Methods
By default, Dirstarter includes 2 authentication methods: Magic Link and Google Login.
Adding Authentication Methods
BetterAuth supports a wide range of authentication methods, including more social providers, email/password, passkeys and more.
Magic Link
Magic Link authentication allows users to sign in without a password. When a user enters their email, they receive a secure link that automatically logs them in.
Social Login
Social login is supported through OAuth providers. Currently configured for Google:
User Roles
Dirstarter implements two user roles:
admin
: Full access to all features and user managementuser
: Standard user access
User Management
Admins have access to various user management actions:
- Edit user details
- Change user roles
- Ban/unban users
- Revoke user sessions
- Delete users
Protecting Routes and Actions
Route Protection
Use middleware to protect routes:
Middleware auth check should not be the only protection for your routes. You should also protect routes in the route handler.
Protecting a page is done by checking the session in the page.
Action Protection
There are 2 predefined server actions procedures that can be used to protect actions:
userProcedure
: Protects actions that require a user to be authenticatedadminProcedure
: Protects actions that require a user to be an admin
You can also create your own procedures and protect them with the same logic. For more information on how to create and use procedures, see the Zod Safe Actions documentation.
To use the procedures in your server actions, create them with the createServerAction
function from ZSA:
Client-Side Usage
Session Management
Admin Checks
Email Templates
Magic link emails use React Email for beautiful, responsive templates:
Always implement proper authorization checks on both the client and server to secure your application.
Last updated on