Overview
The SDK handles the complete authentication lifecycle, from initiating login to exchanging authorization codes for tokens and managing user sessions. All authentication routes are automatically handled by the SDK’s middleware or proxy layer.Authentication Flow Diagram
The following diagram illustrates the complete authentication flow:Flow Steps Explained
1. Login Initiation (/auth/login)
When a user visits /auth/login, the SDK:
- Generates PKCE parameters:
code_verifier: A cryptographically random stringcode_challenge: SHA-256 hash of the code_verifier
- Creates a random
stateparameter to prevent CSRF attacks - Creates a random
nonceto prevent token replay attacks - Stores transaction state in an encrypted cookie
- Redirects the user to Auth0’s
/authorizeendpoint
You can pass additional authorization parameters via query strings:
2. User Authentication
Auth0 handles the authentication process:- Displays the login page (Universal Login)
- Validates user credentials
- Performs MFA if required
- Executes any configured Auth0 Actions or Rules
3. Authorization Callback (/auth/callback)
After successful authentication, Auth0 redirects back to your application with an authorization code. The SDK:
- Validates the state parameter against the stored transaction
- Retrieves the code_verifier from the transaction cookie
- Exchanges the authorization code for tokens:
-
Receives tokens from Auth0:
access_token: Used to call APIsid_token: Contains user profile information (JWT)refresh_token: Used to obtain new access tokens (optional)expires_in: Token expiration time in seconds
-
Validates the ID token:
- Verifies JWT signature using Auth0’s public keys (JWKS)
- Validates the
nonceclaim matches the stored value - Checks issuer (
iss) and audience (aud) claims - Verifies token hasn’t expired (
expclaim) - Validates
max_ageif specified
-
Creates the session:
- Extracts user profile from validated ID token claims
- Stores tokens and user data in session
- Encrypts session data and stores in cookie (stateless) or database (stateful)
-
Cleans up and redirects:
- Deletes the transaction cookie
- Redirects user to the
returnToURL or default path
PKCE Security
PKCE (Proof Key for Code Exchange) prevents authorization code interception attacks: Even if an attacker intercepts the authorization code, they cannot exchange it for tokens without thecode_verifier, which is only stored in your application.
DPoP Support (Optional)
When DPoP (Demonstrating Proof-of-Possession) is enabled, the SDK binds tokens to cryptographic key pairs:- During authorization: The SDK sends
dpop_jkt(JWK thumbprint) parameter - During token exchange: A DPoP proof JWT is sent with each token request
- When using tokens: DPoP proofs are generated for each API request
Learn more about DPoP configuration in the DPoP Examples documentation.
Pushed Authorization Requests (PAR)
When enabled, PAR moves authorization parameters from the URL to a direct backend request:- Keeps sensitive parameters out of browser history and logs
- Prevents parameter tampering
- Supports larger parameter sets
Transaction State Management
The SDK stores temporary authentication state in encrypted cookies during the OAuth flow:- Encrypted using the
AUTH0_SECRET - Automatically deleted after callback completes
- Unique per authentication attempt (when
enableParallelTransactionsis true)
By default, the SDK supports multiple concurrent authentication flows with unique transaction cookies per flow. Set
enableParallelTransactions: false to use a single shared transaction cookie.Error Handling
The SDK provides specific error types for different failure scenarios:| Error | Code | Cause | Solution |
|---|---|---|---|
MissingStateError | missing_state | State parameter not in callback | Check callback URL format |
InvalidStateError | invalid_state | State doesn’t match transaction | Transaction expired or CSRF attack |
AuthorizationError | authorization_error | Auth0 rejected authorization | Check Auth0 logs for details |
AuthorizationCodeGrantError | authorization_code_grant_error | Token exchange failed | Verify client credentials |
OAuth2Error | varies | OAuth 2.0 protocol error | See error_description |
Customizing the Flow
You can customize authentication behavior using hooks and options:Before Session Saved Hook
Modify session data before it’s persisted:On Callback Hook
Customize redirect behavior or handle errors:Next Steps
- Learn about Session Management after authentication
- Explore all Authentication Routes provided by the SDK
- Configure Authorization Parameters