Better Auth configuration
Authentication is configured in two files:Server configuration
lib/auth.ts
Client configuration
lib/auth-client.ts
Security features
Rate limiting
Rate limiting
- 10 requests per minute per IP address
- Prevents brute force attacks on login endpoints
- Returns 429 Too Many Requests when exceeded
Secure cookies
Secure cookies
Session expiry
Session expiry
- Sessions expire after 7 days of inactivity
- Sessions refresh after 1 day of activity
- Automatic cleanup of expired sessions
Password hashing
Password hashing
- Better Auth uses bcrypt for password hashing
- Salted hashes prevent rainbow table attacks
Authentication flows
Registration flow
Registration implementation
app/register/page.tsx
Login flow
Login implementation
app/login/page.tsx
Logout flow
Session management
Session configuration
lib/auth.ts
Session verification
Server-side (Server Component)
app/dashboard/employee/page.tsx
Client-side (Client Component)
components/dashboard/ProfileButton.tsx
Custom user fields
Better Auth is configured to support custom user fields:lib/auth.ts
Field purposes
type
type
Determines dashboard access:
'employee' or 'organization'. Set during registration.profession
profession
User’s profession (e.g., “Biotechnologist”). Required for job matching and profile display.
isActive
isActive
Admin-controlled flag. Inactive users cannot log in (checked during authentication).
avatar
avatar
Profile picture URL. Optional, defaults to generated avatar.
organizationId
organizationId
Links employees to their organization for company-managed accounts.
API endpoint
All authentication requests are handled by a single catch-all route:app/api/auth/[...all]/route.ts
POST /api/auth/sign-in- LoginPOST /api/auth/sign-up- RegistrationPOST /api/auth/sign-out- LogoutGET /api/auth/session- Get current session- Other Better Auth endpoints (password reset, email verification, etc.)
Error handling
Common errors
Invalid credentials
Invalid credentials
Rate limit exceeded
Rate limit exceeded
Duplicate email
Duplicate email
Inactive account
Inactive account
Best practices
- Always use
useSession()on client,auth.api.getSession()on server - Redirect based on user
typeafter login/signup - Check
isActivefield before allowing login (future enhancement) - Use rate limiting to prevent abuse
- Enable secure cookies in production
- Log authentication events for security audits
- Handle all error cases with user-friendly messages