Overview
Your Bible uses Better Auth to provide secure, type-safe authentication. The application supports email-based authentication with optional GitHub OAuth integration.Better Auth Setup
Better Auth is configured with a Drizzle adapter to work seamlessly with PostgreSQL.Server Configuration
The main authentication configuration is located insrc/lib/auth.ts:
Client Configuration
The client-side authentication utilities are insrc/lib/auth-client.ts:
Environment Variables
Add these variables to your.env file:
The
BETTER_AUTH_SECRET should be a long, random string. You can generate one using:Email-Based Authentication
Sign Up
To implement user registration in your components:Sign In
To implement user login:Sign Out
To implement logout functionality:Session Management
Using the Session Hook
Better Auth provides auseSession hook to access the current user’s session:
Session Data Structure
The session object contains:Protected Routes
To protect routes that require authentication, use the session hook to check if a user is logged in:GitHub OAuth (Optional)
Setting Up GitHub OAuth
Create GitHub OAuth App
- Go to GitHub Developer Settings
- Click “New OAuth App”
- Fill in the application details:
- Application name: Your Bible
- Homepage URL:
http://localhost:5173(or your production URL) - Authorization callback URL:
http://localhost:5173/api/auth/callback/github
- Copy the Client ID and generate a Client Secret
Authentication Features
Auto Sign In
The application is configured withautoSignIn: true, which means users are automatically signed in after successful registration. No additional login step is required.
Email Verification
Email verification is currently disabled (requireEmailVerification: false) for a smoother development experience. You can enable it in production:
Database Tables
Better Auth automatically creates and manages the following tables in your PostgreSQL database:users- User account informationsessions- Active user sessionsaccounts- OAuth provider accounts (for GitHub login)verificationTokens- Email verification tokens (if enabled)
These tables are created automatically when you first run the application with Better Auth configured.
Best Practices
-
Secure Secrets: Never commit your
.envfile. KeepBETTER_AUTH_SECRETsecure and unique per environment. - HTTPS in Production: Always use HTTPS in production. Better Auth requires secure connections for cookies.
- Session Expiration: Configure appropriate session expiration times based on your security requirements.
- Error Handling: Always handle authentication errors gracefully and provide clear feedback to users.
- User Data: Only request and store user data that you actually need.
Troubleshooting
Session Not Persisting
If sessions aren’t persisting across page reloads:- Check that
BETTER_AUTH_URLis correctly set - Verify that cookies are enabled in the browser
- Ensure your domain supports cookies (localhost works fine)
Authentication Errors
If you see authentication errors:- Verify all environment variables are set correctly
- Check that the database connection is working
- Ensure
BETTER_AUTH_SECRETis set and consistent
OAuth Callback Issues
For GitHub OAuth problems:- Verify the callback URL in GitHub OAuth settings matches your application
- Check that
GITHUB_CLIENT_IDandGITHUB_CLIENT_SECRETare correct - Ensure the GitHub OAuth app is active