Endpoint
Request Body
The refresh token obtained during session creation or previous refresh. This is an opaque token that should be stored securely on the client.
The anti-CSRF token associated with the refresh token (required if anti-CSRF is enabled).
Whether anti-CSRF protection is enabled for this session. Must match the value used during session creation.
Whether to use dynamic signing keys for the new access token. Available in CDI >= 3.0.
true- Uses rotating keys for enhanced security (default for CDI < 5.0)false- Uses static key for simplified verificationnull- Uses the existing session’s signing key preference
Response
Success Response
Returns
"OK" on successful refresh.Session metadata
New access token
New refresh token
Error Responses
Error status code:
"UNAUTHORISED"- Invalid or expired refresh token"TOKEN_THEFT_DETECTED"- Refresh token reuse detected
Error description explaining why the refresh failed.
Session information when token theft is detected (only for
TOKEN_THEFT_DETECTED status)Example Request
Example Responses
Successful Refresh
Unauthorized Response
Token Theft Detected
Implementation Details
Source Code Reference
Implemented in:- API Handler: View source
- Session Logic: View source
Refresh Process
- Validate Refresh Token - Decrypts and verifies the refresh token structure
- Check Anti-CSRF - Validates anti-CSRF token if enabled
- Database Transaction - Atomic operation to prevent race conditions
- Verify Token Chain - Checks refresh token hash matches database
- Detect Token Theft - Identifies if an old refresh token was reused
- Rotate Tokens - Creates new access and refresh tokens
- Update Database - Stores new refresh token hash
- Update Active Users - Records user activity timestamp
Token Rotation
SuperTokens implements automatic refresh token rotation:- Each refresh creates a new refresh token
- The old refresh token becomes invalid
- New tokens include reference to parent token hash
- Database tracks the current valid refresh token hash
Token Theft Detection
Token theft is detected when:- A refresh token is used that doesn’t match the current valid token in the database
- The token also doesn’t match the parent token (normal rotation scenario)
- This indicates an old token was stolen and reused
- All sessions for the user are immediately revoked
TOKEN_THEFT_DETECTEDresponse is returned- The session handle and user IDs are provided for logging
Security Considerations
Token Rotation Security
Anti-CSRF Protection
When anti-CSRF is enabled, a new anti-CSRF token is generated with each refresh. Update the stored anti-CSRF token on the client.
Token Theft Response
Recommended actions on token theft detection:- Log the Event - Record session handle, user ID, IP address, and timestamp
- Alert Security - Notify security monitoring systems
- User Notification - Consider emailing the user about suspicious activity
- Force Logout - Clear all client-side session data
- Require Re-authentication - User must log in again
Common Use Cases
Automatic Token Refresh
Handling Refresh in API Calls
Error Handling
Unauthorized Errors
Common causes:- Refresh token has expired
- Session was revoked
- Refresh token is invalid or corrupted
- Anti-CSRF token doesn’t match
- Session not found in database
Token Theft Detection
Causes:- Old refresh token was reused after new one was issued
- Indicates potential token theft or client-side storage issues
- Clear all session data
- Log security event
- Show security alert to user
- Redirect to login
Best Practices
- Refresh Proactively - Refresh tokens before access token expires (e.g., 5 minutes before)
- Handle Failures Gracefully - On refresh failure, log out user rather than retrying
- Secure Storage - Store refresh tokens in httpOnly cookies or secure storage
- Monitor Theft Detection - Alert on TOKEN_THEFT_DETECTED responses
- Update Atomically - Update all tokens (access, refresh, anti-CSRF) together
- Rate Limit - Implement rate limiting to prevent refresh token brute force
CDI Version Compatibility
- CDI < 2.21: Returns
idRefreshTokenin response - CDI >= 2.21:
idRefreshTokenremoved from response - CDI >= 3.0:
useDynamicSigningKeyparameter available,tenantIdin response - CDI >= 4.0:
recipeUserIdincluded in session response - CDI >= 5.0:
useDynamicSigningKeydefaults tonullinstead oftrue