Session Strategies
Arraf Auth supports two session strategies: JWT and database. You can choose the strategy that best fits your application’s requirements.- JWT Strategy
- Database Strategy
JSON Web Tokens (JWT) store session data in a signed token, eliminating the need for database lookups on every request.Configuration:How it works:
- After authentication, a session record is created in the database
- A JWT is generated containing
userId,sessionId, andemail(seepackages/core/src/session.ts:28-29) - The JWT is signed with your secret key and stored in a cookie
- On subsequent requests, the JWT is verified and decoded
- The session is validated against the database to ensure it hasn’t been revoked
- Faster: No database lookup to read session data
- Stateless: Token contains all necessary information
- Scalable: Works well with distributed systems
- Sessions are still stored in the database for revocation support
- JWT verification happens on every request (
packages/core/src/session.ts:54-61)
Both strategies create a session record in the database. The difference is whether the cookie contains a JWT (strategy: ‘jwt’) or a random token (strategy: ‘database’).
Session Configuration
TheSessionConfig interface provides options to customize session behavior:
packages/core/src/types.ts:84-94 for the complete interface definition.
Cookie Options
secure
secure
When
true, the cookie is only sent over HTTPS connections. Always use true in production.httpOnly
httpOnly
When
true (default), the cookie cannot be accessed via JavaScript, protecting against XSS attacks.sameSite
sameSite
Controls when cookies are sent with cross-site requests:
strict: Cookie is never sent on cross-site requestslax: Cookie is sent on top-level navigation (default, recommended)none: Cookie is sent on all requests (requiressecure: true)
domain
domain
Specifies which domains can receive the cookie. Useful for sharing sessions across subdomains.
Session Lifecycle
Creating Sessions
Sessions are automatically created after successful authentication in all flows (phone+OTP, email+password, OAuth). TheSessionManager.createSession() method handles:
- Generating a secure random token
- Calculating expiration time (default: 30 days from now)
- Storing session metadata (IP address, user agent)
- Creating the session record in database
- Generating and signing the cookie value
- Serializing the cookie with appropriate options
Retrieving Sessions
Retrieve the current user’s session using theSessionManager.getSession() method:
packages/core/src/session.ts:47-69):
- Extracts the cookie from request headers
- For JWT strategy: Verifies and decodes the JWT
- For database strategy: Uses the token directly
- Looks up the session in the database
- Checks if the session has expired
- Fetches the associated user record
- Returns both user and session, or
nullif invalid
Even with JWT strategy, the session is still validated against the database to support session revocation.
Revoking Sessions
Delete a session to log out a user:packages/core/src/session.ts:71-80 for the implementation.
Revoking All User Sessions
Revoke all sessions for a specific user (useful for security events):- User changes their password
- Suspicious activity is detected
- User explicitly requests to log out from all devices
Session Schema
TheSession interface defines the structure of session records:
packages/core/src/types.ts:13-21 for the complete interface.
Security Best Practices
Secure Secret Management
Session Expiration
Sessions automatically expire based on theexpiresIn configuration. The default is 30 days. Currently, this is hard-coded in packages/core/src/session.ts:82-85, but can be customized in future versions.
IP and User Agent Tracking
Sessions automatically capture IP address and user agent for security monitoring:- Detect suspicious login locations
- Identify unauthorized access
- Display active sessions to users
Middleware Integration
Use sessions in your middleware to protect routes:- Next.js
- Express
Next Steps
Authentication Flows
Learn about phone+OTP, email+password, and OAuth flows
Database Adapters
Understand how sessions are stored in your database