Authentication System
The auth configuration is located insrc/lib/server/auth.js:
Better Auth provides database-backed sessions, CSRF protection, and secure password hashing out of the box.
Login Flow
Login Page
The login interface is at/login (src/routes/login/+page.svelte):
Visit /login
Users are presented with a clean login form featuring:
- Email input field
- Password input field
- “Forgot password” link
- Submit button
Server Validation
Better Auth validates credentials against the database and creates a session if successful.
Login UI Features
Auto-dismissing Errors
Error messages automatically fade after 5 seconds:
Loading States
Button disables during submission with loading text to prevent double-submission.
Glass Morphism Design
Modern UI with backdrop blur and subtle gradients:
Return to Site Link
Users can navigate back to the public homepage from the login screen.
Session Management
Session Configuration
Why 1-hour sessions without auto-refresh?
Why 1-hour sessions without auto-refresh?
This configuration ensures:
- Security: Sessions expire after 1 hour of creation, not last activity
- Explicit re-authentication: Users must log in again after timeout
- Audit trail clarity: Session timestamps are unambiguous
expiresIn and updateAge values.Session Storage
Sessions are stored in the PostgreSQL database via Drizzle ORM:Rate Limiting
Protection against brute force attacks with configurable rate limits:Password Reset
Forgot Password Flow
Email Configuration
Requires environment variables:Use a Gmail App Password, not your regular password. Generate one at Google Account Settings.
Route Protection
Admin Layout Guard
All admin routes are protected viasrc/routes/admin/+layout.server.js:
How It Works
- User requests
/adminor any sub-route - SvelteKit runs the layout server load function
- Auth hooks (not shown) populate
event.locals.userfrom session cookie - If no user exists, redirect to
/login - If user exists, load proceeds and admin UI renders
Logout
Logout is handled via form submission in the sidebar:+page.server.js) destroys the session and redirects to the login page.
User Display
The sidebar shows logged-in user information:- Avatar: Circular badge with user initials
- Name: Full user name
- Role: “Panel de Control” label
Session Security
- HTTP-only cookies prevent XSS access
- CSRF tokens on all form submissions
- Secure session storage in database
Password Security
- Passwords hashed with bcrypt
- Minimum length enforced
- No plaintext storage
Environment Variables
Required for authentication:Security Features
CSRF Protection
CSRF Protection
Better Auth automatically includes CSRF tokens in forms and validates them on submission.
Rate Limiting
Rate Limiting
IP-based rate limits prevent brute force attacks on login and password reset endpoints.
Secure Cookies
Secure Cookies
Session Expiry
Session Expiry
Sessions expire after 1 hour and require re-authentication.
Password Hashing
Password Hashing
All passwords are hashed using bcrypt before database storage.
Extending Authentication
Better Auth supports additional features you can enable:- OAuth providers (Google, GitHub, etc.)
- Two-factor authentication (2FA)
- Magic link authentication
- Role-based access control (RBAC)
- Account linking
