Overview
The authentication system provides:- Email/password authentication with validation
- Email verification for new accounts
- Password reset flow
- Session management with automatic refresh
- Rate limiting for security
- Organization support with invitations
- Admin impersonation capabilities
- Comprehensive audit logging
Authentication Setup
The auth configuration is centralized insrc/lib/auth/auth.ts. Two instances are created:
- API Instance (async): Used by the Astro application
- CLI Instance (sync): Used by command-line tools and migrations
Core Configuration
Plugins
Concordia uses three Better Auth plugins to extend functionality.Username Plugin
Enables username-based authentication in addition to email:- Allows users to sign in with username or email
- Usernames are validated during signup
- Unique constraint enforced at database level
Organization Plugin
Provides multi-tenant organization support:auth.ts:318-334):
Admin Plugin
Enables administrative capabilities:- User impersonation for support
- Direct user management
- Access to all organization data
Email Verification
Email verification is required for all new accounts.Verification Flow
1
User signs up
User submits email, password, and optional username/name
2
Account created
User account is created with
emailVerified: false3
Verification email sent
System sends verification email with unique token link
4
User clicks link
User clicks verification link in email
5
Email verified
Account is marked as verified, user can fully access the platform
auth.ts:78-90):
Password Reset
Users can reset their password via email.Reset Flow
1
Request reset
User enters email address on forgot password page
2
Reset email sent
System sends password reset email with secure token
3
User clicks link
User clicks reset link (valid for limited time)
4
Set new password
User enters and confirms new password
5
Password updated
Password is updated, user can log in with new credentials
auth.ts:65-76):
Session Management
Sessions are managed with secure cookies and automatic refresh.Session Configuration
- Sessions expire after 7 days of inactivity
- Session tokens are automatically refreshed every 24 hours
- Cookie cache reduces database queries
- Absolute timeout ensures re-authentication after 7 days
Session Security
- Secure cookies (HTTPS only in production)
- Origin checking enabled
- Trusted origins whitelist
- IP tracking for audit logs
- Bearer token invalidation on logout
Rate Limiting
Built-in rate limiting protects against brute force attacks.- Global: 100 requests per minute per IP
- Sign in: 5 attempts per 15 minutes
- Sign up: 10 registrations per hour
Post-Signup Hooks
When a user signs up, several actions occur automatically:Automatic Profile Creation
Automatic Profile Creation
Every new user gets a profile with:
- Auto-generated username (from email if not provided)
- Preferred language set to French (“fr”)
- Empty bio and optional full name
Wallet Initialization
Wallet Initialization
Every user receives a digital wallet:
- Starting balance: 0.00 EUR
- Ready for transactions, bookings, and donations
Default Role Assignment
Default Role Assignment
All users are automatically granted the citizen role:
- Enables core platform features
- Additional roles can be granted by admins
Audit Logging
Audit Logging
Signup events are logged:
- User ID and email recorded
- Timestamp captured
- Available for compliance and debugging
Audit Logging
All authentication events are logged to theaudit_log table.
Login Success
Login Failure
signup- New user registrationlogin_success- Successful authenticationlogin_failed- Failed login attempt (with IP and user agent)
Usage Examples
Get Current Session
Sign Up New User
Sign In
Request Password Reset
Sign Out
Security Best Practices
1
Always verify email
Email verification is required - do not disable it in production.
2
Use strong passwords
Password validation ensures minimum complexity requirements.
3
Monitor rate limits
Check rate limit metrics to detect potential attacks.
4
Review audit logs
Regularly review
audit_log for suspicious activity.5
Secure cookies in production
Ensure
useSecureCookies: true and HTTPS is enforced.