Overview
Ave uses session tokens to maintain authenticated state across requests. Sessions are created after successful authentication and validated on each protected endpoint.Session Architecture
Session Tokens
Session tokens are cryptographically random 64-character hexadecimal strings:ave-server/src/lib/crypto.ts:61-63
Properties:
- 256 bits of entropy - Cryptographically secure random generation
- Stored as SHA-256 hash - Server never stores plaintext tokens
- Single-use on creation - Token returned once then hashed
- Stateless validation - No session store required, database lookup only
ave-server/src/lib/crypto.ts:66-68
Session Storage
Sessions are persisted in the database with metadata:ave-server/src/routes/login.ts:260-267
Session lifetime:
- Default: 30 days from creation
- Configurable per-deployment
- No automatic renewal (requires re-authentication)
ave-server/src/routes/login.ts:259
Session Creation
After Successful Authentication
Sessions are created after passkey, trust code, or device approval login:ave-server/src/routes/login.ts:257-269 and ave-server/src/routes/login.ts:602-613
Device Association
Each session is tied to a specific device:- Device fingerprinting - Client generates stable fingerprint from browser/OS
- Device reuse - Same fingerprint reuses existing device record
- Device tracking - Each login updates
lastSeenAttimestamp
ave-server/src/routes/login.ts:29-88
Session Cookies
Cookie Configuration
Ave sets HTTP-only cookies for browser-based authentication:ave-server/src/lib/session-cookie.ts:25-35
Cookie name: ave_session
See: ave-server/src/lib/session-cookie.ts:4
Secure Context Detection
Cookies adapt to deployment environment:ave-server/src/lib/session-cookie.ts:15-23
Development: secure: false, sameSite: LaxProduction:
secure: true, sameSite: None
Domain Scoping
Cookie domain is set for subdomain sharing:ave-server/src/lib/session-cookie.ts:6-13
Examples:
localhost:5173→ No domain (current host only)app.aveid.net→.aveid.net(shared with api.aveid.net, etc.)- Custom deployment →
COOKIE_DOMAINenvironment variable
Session Validation
Authentication Middleware
Protected routes userequireAuth middleware:
ave-server/src/routes/security.ts:23
Token Extraction
Middleware checks multiple token sources:- Authorization header:
Bearer <token> - Cookie header:
ave_session=<token>
- Browser requests (cookie-based)
- API clients (Bearer token)
Validation Process
- Token never stored in plaintext
- Constant-time comparison (via database lookup)
- Expiration checked on every request
- No token renewal (prevents session fixation)
Session Termination
Explicit Logout
User-initiated logout:ave-server/src/routes/login.ts:742-764 and ave-server/src/lib/session-cookie.ts:37-42
Device Revocation
Revoking a device terminates all its sessions:TESTING.md:348-355
Revoke All Devices
Emergency logout from all devices:TESTING.md:352-355
Automatic Expiration
Sessions expire automatically after 30 days:- No background cleanup job required
- Expiration checked during validation
- Expired sessions remain in database until next cleanup
- Optional: Add cron job to delete old sessions
Expired sessions fail validation automatically. Consider periodic cleanup for database hygiene.
Security Features
Token Generation
Cryptographically secure randomness:ave-server/src/lib/crypto.ts:61-63
Entropy: 2^256 possible tokens (practically unguessable)
Hash-Only Storage
Server never stores plaintext tokens:ave-server/src/lib/crypto.ts:66-68
Benefits:
- Database breach doesn’t expose valid tokens
- Rainbow table attacks ineffective (random input)
- Tokens can’t be extracted even with database access
HTTP-Only Cookies
JavaScript cannot access session cookies:ave-server/src/lib/session-cookie.ts:28
Protection against:
- XSS attacks stealing tokens
- Malicious scripts reading cookies
- Client-side token exposure
HTTPS Enforcement
Secure cookies in production:ave-server/src/lib/session-cookie.ts:29-30
Protection against:
- Man-in-the-middle attacks
- Network sniffing
- HTTP downgrade attacks
SameSite Protection
CSRF protection via SameSite:- Development:
sameSite: Lax(blocks cross-site POST) - Production:
sameSite: None(allows cross-origin with HTTPS)
ave-server/src/lib/session-cookie.ts:30
Device Binding
Each session is bound to a device record:- Track where sessions are used
- Revoke access per-device
- Detect suspicious activity (IP/UA changes)
- Granular access control
ave-server/src/routes/login.ts:260-267
Activity Logging
Session Creation Events
All logins are logged with method and device info:ave-server/src/routes/login.ts:272-280
Severity levels:
info- Passkey or device approval loginwarning- Trust code login (recovery method)
Viewing Activity Logs
Users can review all session activity:- Navigate to Dashboard → Activity Log
- Filter by severity (Info, Warning, Danger)
- Search for specific actions (“login”, “passkey”, etc.)
- Review timestamps, IP addresses, and device info
TESTING.md:396-421
Best Practices
For Application Developers
- Always use HTTPS in production - Set
COOKIE_SECURE=true - Configure COOKIE_DOMAIN - Enable subdomain sharing if needed
- Set RP_ORIGIN correctly - Must match actual frontend origin
- Implement token rotation - Consider shorter expiration with renewal
- Add rate limiting - Prevent brute-force session validation
- Monitor failed logins - Alert on suspicious authentication patterns
- Clean up expired sessions - Run periodic database cleanup
For End Users
- Log out on shared devices - Always click logout on public computers
- Review active devices - Check Dashboard → Devices regularly
- Revoke suspicious sessions - Use device revocation if anything looks wrong
- Monitor activity log - Watch for unexpected login locations/times
- Use trusted networks - Avoid public WiFi for sensitive operations
Troubleshooting
”Invalid or expired session” Error
Possible causes:- Session expired - More than 30 days since creation
- Token revoked - Device or session manually revoked
- Token mismatch - Browser sent wrong cookie or header
- Database cleared - Sessions deleted during maintenance
- Log in again to create new session
- Check cookie is being sent (DevTools → Network → Cookies)
- Verify
Authorizationheader format:Bearer <token> - Clear cookies and try fresh login
Session Not Persisting
Possible causes:- Cookie blocked - Browser privacy settings blocking cookies
- Domain mismatch - Cookie domain doesn’t match request domain
- SameSite restrictions - Cross-site cookie blocked
- Secure flag mismatch - Secure cookie over HTTP
- Check browser console for cookie warnings
- Verify
COOKIE_DOMAINenvironment variable - Ensure HTTPS in production
- Test with
COOKIE_SECURE=falsein development
ave-server/src/lib/session-cookie.ts:6-23
Session Works on Postman but Not Browser
Likely cause: Cookie configuration issue- Check if browser blocks third-party cookies
- Verify
sameSiteattribute matches context - Ensure
domainattribute is correct - Try using
Authorization: Bearerheader instead
Multiple Sessions for Same Device
If the same device creates multiple session records: Possible causes:- Fingerprint not sent - Client not providing
fingerprintfield - Fingerprint changing - Browser generates different fingerprint each time
- Device record deleted - Previous device manually removed
fingerprint in device info
See: ave-server/src/routes/login.ts:34-45
Environment Variables
Session Configuration
ave-server/src/lib/session-cookie.ts:8,16
Related Topics
- Trust Codes - Recovery with trust codes
- Recovery Methods - All authentication methods
- Best Practices - Security recommendations