Overview
RLink uses Better Auth for authentication, providing a secure, modern authentication system with support for:- Email/Password authentication
- Two-Factor Authentication (2FA)
- Session management
- Password reset flow
- Account verification
Authentication Flow
Configuration
Environment Variables
Better Auth Setup
Better Auth is wired through the Next.js App Router. In the RLink repository, look for:- A catch-all auth route under
app/api/auth/(Better Auth’s usual pattern) - Client helpers (for example
lib/auth-client.tsor similar) used by the login and dashboard
- Email/Password authentication
- Session-based authentication
- Admin plugin for user management
- Two-Factor Authentication plugin
User Authentication
Login Process
Users can log in via the/login page:
- Enter Credentials: Email and password
- 2FA Check: If 2FA is enabled, prompt for code
- Session Creation: On success, create session cookie
- Redirect: Send user to
/homedashboard
Session Management
Sessions are stored in the database and validated on each request. Session Duration: Configured in Better Auth settings Session Storage:- Database table:
sessions(Better Auth table) - Cookie name: Set by Better Auth (typically
better-auth.session_token) - Cookie attributes:
HttpOnly,Secure(in production),SameSite=Lax
Protected Routes
Routes are protected using theProtectedRoute component:
Two-Factor Authentication (2FA)
Setup Process
Users can enable 2FA in Settings → Privacy & Security:2FA Login Flow
When 2FA is enabled:- User enters email/password
- Password validated successfully
- System detects 2FA enabled
- User prompted for 6-digit TOTP code
- Code validated (30-second window)
- Session created on success
Supported Authenticator Apps
- Google Authenticator
- Microsoft Authenticator
- Authy
- 1Password
- Any TOTP-compatible app
Backup Codes
Backup codes (if enabled in your Better Auth configuration) are typically issued when 2FA is first enabled so users can sign in without the authenticator app.Password Reset Flow
Request Reset
- User clicks “Forgot Password” on login page
- Enters email address
- System sends reset email via Resend
- Email contains unique reset token (time-limited)
Reset Password
- User clicks link in email
- Redirected to
/reset-password?token=xxx - Enters new password (must meet strength requirements)
- Password updated in database
- All existing sessions invalidated
- User redirected to login
Email Template
Reset emails use the React Email template system:User Registration
Admin-Only Registration
Self-registration is disabled for security purposes (as noted in changelog v0.0.8). New users can only be created by administrators through the IAM module:Session Security
Security Measures
Secure Cookies
Secure Cookies
Password Hashing
Password Hashing
Better Auth uses bcrypt for password hashing with appropriate salt rounds
Session Expiration
Session Expiration
Sessions expire after period of inactivity (configured in Better Auth)
CSRF Protection
CSRF Protection
Built-in CSRF protection via SameSite cookies and session validation
Rate Limiting
Rate Limiting
Login attempts should be rate-limited to reduce brute-force risk (configure in Better Auth or at the edge).
Session Validation
Every API request validates the session:Password Requirements
Based on the changelog (v0.0.9), the system includes password validation with strength meter: Requirements (typical):- Minimum 8 characters
- At least one uppercase letter
- At least one lowercase letter
- At least one number
- At least one special character
- Weak: Minimum requirements only
- Medium: Good length, character variety
- Strong: Excellent length, high entropy
API Endpoints
POST /api/auth/login
Authenticate user and create session. Request:POST /api/auth/verify-2fa
Verify 2FA code and complete login. Request:POST /api/auth/logout
Terminate current session. Response:POST /api/auth/forgot-password
Request password reset email. Request:POST /api/auth/reset-password
Reset password with token. Request:Client-Side Usage
Using the auth client
Use the project’s generated Better Auth client (seelib/ in the repo) for session state:
Checking Auth Status
Best Practices
Never Store Passwords
Always hash passwords, never store plaintext
Use HTTPS
Always use HTTPS in production for secure cookie transmission
Validate Sessions
Validate session on every protected API request
Rotate Secrets
Rotate
BETTER_AUTH_SECRET periodically (invalidates all sessions)Troubleshooting
Common authentication issues and solutions are in Troubleshooting — Authentication issues.Next Steps
Architecture
IAM boundaries and data flow
Authorization
Module access and
403 behaviorAPI overview
Sessions, CORS, and how routes are documented
Dashboard overview
Where users work after sign-in
