Skip to main content
The Auth0 Next.js SDK implements the OAuth 2.0 Authorization Code Flow with PKCE (Proof Key for Code Exchange) to securely authenticate users in your Next.js applications.

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 string
    • code_challenge: SHA-256 hash of the code_verifier
  • Creates a random state parameter to prevent CSRF attacks
  • Creates a random nonce to prevent token replay attacks
  • Stores transaction state in an encrypted cookie
  • Redirects the user to Auth0’s /authorize endpoint
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:
  1. Validates the state parameter against the stored transaction
  2. Retrieves the code_verifier from the transaction cookie
  3. Exchanges the authorization code for tokens:
  1. Receives tokens from Auth0:
    • access_token: Used to call APIs
    • id_token: Contains user profile information (JWT)
    • refresh_token: Used to obtain new access tokens (optional)
    • expires_in: Token expiration time in seconds
  2. Validates the ID token:
    • Verifies JWT signature using Auth0’s public keys (JWKS)
    • Validates the nonce claim matches the stored value
    • Checks issuer (iss) and audience (aud) claims
    • Verifies token hasn’t expired (exp claim)
    • Validates max_age if specified
  3. 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)
  4. Cleans up and redirects:
    • Deletes the transaction cookie
    • Redirects user to the returnTo URL or default path
The callback route must be registered in your Auth0 Application’s Allowed Callback URLs settings in the Auth0 Dashboard.

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 the code_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:
With DPoP enabled, the flow includes:
  1. During authorization: The SDK sends dpop_jkt (JWK thumbprint) parameter
  2. During token exchange: A DPoP proof JWT is sent with each token request
  3. When using tokens: DPoP proofs are generated for each API request
This prevents token theft since tokens are bound to the application’s private key.
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:
Standard Flow:
PAR Flow:
Benefits:
  • 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:
Transaction cookies are:
  • Encrypted using the AUTH0_SECRET
  • Automatically deleted after callback completes
  • Unique per authentication attempt (when enableParallelTransactions is 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:
Common errors:

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