Auth0
Configure Auth0 with Serverless API Gateway. Set up Auth0 client ID, client secret, JWKS, callback URLs, and OAuth 2.0 authorization flow.
Integrate Auth0 with the Serverless API Gateway to add OAuth 2.0 / OpenID Connect authentication to your API. This page covers the required configuration parameters, environment variables, path setup, and the authorization flow.
For general authentication concepts and comparison with other providers, see the Authentication Guide. For JWT-based authorization without a third-party provider, see Authorizer.
Prerequisites
Before configuring the Serverless API Gateway, create an Auth0 application:
Log in to your Auth0 Dashboard
Go to Applications and click Create Application
Choose Regular Web Application or Single Page Application depending on your use case
Note the Domain, Client ID, and Client Secret from the application settings
Under Allowed Callback URLs, add your gateway callback URL (e.g.
https://your-api-url/api/v1/auth0/callback)
Configuration Parameters
{
"authorizer": {
"type": "auth0",
"domain": "your-auth0-domain.auth0.com",
"client_id": "your-client-id",
"client_secret": "your-client-secret",
"redirect_uri": "https://your-api-url/api/v1/auth0/callback",
"jwks": "{JSON Escaped JWKS}",
"jwks_uri": "https://your-auth0-domain.auth0.com/.well-known/jwks.json",
"scope": "openid profile email"
}
}Parameters Explained
type: Specifies the type of authorizer being used. In this case, it is set to "auth0".
domain: The Auth0 domain associated with your account. Replace
your-auth0-domainwith your actual Auth0 domain.client_id: The unique identifier for your Auth0 application. Found in the Auth0 Dashboard under Applications > your app > Settings. Replace
your-client-idwith your actual client ID.client_secret: The secret key associated with your Auth0 application. This value should be stored as a Wrangler secret, not in plain text. Replace
your-client-secretwith your actual client secret.redirect_uri: The URI to which Auth0 will redirect users after authentication. This must match one of the Allowed Callback URLs in your Auth0 application settings.
jwks: A JSON Web Key Set (JWKS) containing the public keys used to verify the JWT signatures. Replace the values in the
n,kid,x5t, andx5cfields with your actual key values. Eitherjwksorjwks_uriis required.jwks_uri: The URI to retrieve the JWKS from Auth0. Replace
your-auth0-domainwith your actual Auth0 domain. Eitherjwksorjwks_uriis required.scope: The permissions being requested from the user. Common scopes include
openid,profile, andemail.
Environment Variables and Secrets
Store sensitive Auth0 credentials securely:
Reference these in your api-config.json using the $env. and $secrets. prefixes:
Important Notes
Ensure that sensitive information such as
client_secretis stored securely usingwrangler secret putand not exposed in public repositories or logs.Update the placeholders in the configuration with your actual Auth0 account details before deployment.
Test the configuration in a safe environment before moving to production.
Path Configurations
This section outlines the path configuration for handling the Auth0 OAuth flow. Add these paths to the paths array in your api-config.json.
Callback Handler
This integration handles the callback from Auth0, receiving an authorization code. It exchanges this code for access and ID tokens and returns them to the client.
Login Redirect
This integration facilitates the redirection to the Auth0 login page (/authorize), allowing users to authenticate via Auth0. It provides a seamless way to initiate the login process based on the Auth0 configuration.
User Info
This integration retrieves user information from Auth0 using the /userinfo endpoint. It allows applications to access user profile data after successful authentication.
Auth0 OAuth Flow
The complete authorization flow with Serverless API Gateway and Auth0 works as follows:
Initiate Login -- Direct the user to your
/api/v1/auth0/callback-redirectendpoint. This redirects them to the Auth0 login page.User Authenticates -- The user logs in through Auth0 (username/password, social login, etc.).
Callback -- Auth0 redirects back to your
/api/v1/auth0/callbackendpoint with an authorization code.Token Exchange -- The gateway exchanges the authorization code for access and ID tokens automatically.
Access Protected Routes -- Use the returned token in the
Authorization: Bearer <token>header to call endpoints that have"auth": true.
CORS Configuration
If your frontend calls the Auth0 endpoints from a browser, configure CORS in your api-config.json:
Related Pages
Authentication Guide -- full authentication setup overview
Authorizer Configuration -- JWT and provider-based authorization details
CORS Configuration -- cross-origin request settings
Supabase OTP Integration -- alternative authentication with Supabase
Auth0 Quickstart -- step-by-step Auth0 quickstart
Auth0 Documentation -- official Auth0 docs
Last updated