Overview
The platform uses JSON Web Tokens (JWT) for session management with NextAuth.js. Sessions are configured with a 30-day expiration and include user information, authentication tokens, and custom claims.Session Configuration
Sessions are configured insrc/auth.ts:27:
Configuration Parameters
Maximum session duration in seconds (30 days). After this period, users must re-authenticate.
Session update interval in seconds (12 hours). The session is refreshed every 12 hours to extend its lifetime.
JWT token maximum age in seconds (30 days). Must match or exceed session.maxAge.
JWT Token Structure
The JWT token is created and enriched in thejwt callback (src/auth.ts:55):
Token Claims
User’s database ID from the
user tableSubject - OAuth provider’s unique identifier for the user
JWT ID - unique identifier for this specific token
Issued At - Unix timestamp when the token was created
Expiration - Unix timestamp when the token expires (30 days from iat)
OAuth access token for making API calls to Google/GitHub
OAuth refresh token (only for admin users) - used to obtain new access tokens
Session Object Structure
The session callback (src/auth.ts:87) transforms the JWT into the session object:
Session Schema
User information and authentication data
ISO 8601 formatted expiration date/time
Complete JWT token object (contains all JWT claims)
Accessing Sessions
Server Components
Server Actions
API Routes
Client Components (using next-auth/react)
Session API Endpoints
Get Current Session
Session Lifecycle
Session Refresh
Sessions are automatically refreshed based on theupdateAge configuration:
- updateAge: 43200 (12 hours)
- When a session is accessed and more than 12 hours have passed since last update, NextAuth automatically refreshes it
- The refresh extends the session lifetime by resetting the expiration time
- This happens transparently without user interaction
Admin Sessions
Admin users (identified byADMIN_EMAIL environment variable) receive additional privileges:
Google OAuth refresh token stored in both JWT and database
Admin Detection
Checking Admin Status
Session Security
HTTP-Only Cookies
HTTP-Only Cookies
Encryption
Encryption
JWT tokens are encrypted using NextAuth’s built-in encryption, ensuring sensitive data like access tokens cannot be read by clients.
Secure Flag
Secure Flag
In production, session cookies use the Secure flag, ensuring they’re only transmitted over HTTPS.
SameSite Protection
SameSite Protection
Cookies are configured with SameSite attribute to prevent CSRF attacks.
Token Rotation
Token Rotation
Sessions are automatically refreshed every 12 hours, limiting the window of opportunity if a token is compromised.
Redirect Configuration
Custom redirect logic is defined insrc/auth.ts:106:
- After successful sign-in, users are redirected to
/inicio - If a return URL is specified and matches the base URL, that URL is used instead
- This prevents open redirect vulnerabilities
Custom Pages
Session-related pages are customized in the configuration (src/auth.ts:111):
Custom sign-in page path
Custom sign-out page (empty string uses default)
Error page for authentication failures
Testing Sessions
Get Session Info
Check Token Expiration
Common Issues
Session undefined in client component
Session undefined in client component
Solution: Wrap your app with
<SessionProvider> from next-auth/react to enable client-side session access.Session not persisting
Session not persisting
Solution: Ensure cookies are enabled in the browser and the domain/path settings are correct. Check that NEXTAUTH_URL is properly configured.
Session expires too quickly
Session expires too quickly
Solution: Check
session.maxAge and jwt.maxAge configuration. Ensure they’re set to the desired duration in seconds.Access token expired
Access token expired
Solution: Implement token refresh logic using the refresh token (admin only) or require the user to re-authenticate.
Next Steps
Google OAuth
Learn about Google OAuth implementation
Authentication Overview
Back to authentication overview