Skip to main content
Concordia uses Better Auth for authentication and session management, configured with email/password authentication, organizations, and advanced security features.

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 in src/lib/auth/auth.ts. Two instances are created:
  1. API Instance (async): Used by the Astro application
  2. 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:
Features:
  • 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:
Organization API (from auth.ts:318-334):

Admin Plugin

Enables administrative capabilities:
Admin 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: false
3

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
Email template (from 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
Reset email template (from auth.ts:65-76):

Session Management

Sessions are managed with secure cookies and automatic refresh.

Session Configuration

Session lifecycle:
  • 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

Security features:
  • 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.
Rate limit windows:
  • Global: 100 requests per minute per IP
  • Sign in: 5 attempts per 15 minutes
  • Sign up: 10 registrations per hour
Rate limit counters are stored in the database for persistence across restarts.

Post-Signup Hooks

When a user signs up, several actions occur automatically:
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
Every user receives a digital wallet:
  • Starting balance: 0.00 EUR
  • Ready for transactions, bookings, and donations
All users are automatically granted the citizen role:
  • Enables core platform features
  • Additional roles can be granted by admins
Signup events are logged:
  • User ID and email recorded
  • Timestamp captured
  • Available for compliance and debugging

Audit Logging

All authentication events are logged to the audit_log table.

Login Success

Login Failure

Logged events:
  • signup - New user registration
  • login_success - Successful authentication
  • login_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.

Environment Variables

Required configuration:

See Also