Documentation Index
Fetch the complete documentation index at: https://auth0.com/llms.txt
Use this file to discover all available pages before exploring further.
Get Started
This guide demonstrates how to integrate Auth0, add authentication, and display user profile information in an Express.js web application using the@auth0/auth0-express SDK.
Create a new project
Create a new directory for your Express application and initialize a Node.js project.Update your
package.json to use ES modules and add start scripts:Configure Auth0
You need to create a new application on your Auth0 tenant and configure your environment variables.
- Quick Setup
- CLI
- Dashboard
Once your app is created, add these values to your Copy the output and use it as the
.env file:Generate a secure session secret:AUTH0_SESSION_SECRET value.On macOS or Linux you can also run
openssl rand -hex 32. The Node command works on every platform, since Node is already a prerequisite.Configure the middleware
Add the What this does:
createAuth0() middleware to your Express application. The SDK automatically mounts /auth/login, /auth/logout, /auth/callback, and /auth/backchannel-logout routes.server.js
createAuth0()reads credentials from environment variables (AUTH0_DOMAIN,AUTH0_CLIENT_ID, etc.) automatically- Mounts four auth routes under
/auth/ - Attaches
req.auth0.clientto every request for session and token access
Add login, logout, and a protected profile route
Protect routes using the Key points:
requiresAuth middleware from the SDK, and display user profile data via getUser().server.js
requiresAuth()from@auth0/auth0-expressprotects routes — unauthenticated users are redirected to/auth/loginreq.auth0.client.getUser()returns the authenticated user’s profile- Login link points to
/auth/login, logout to/auth/logout— both are automatically mounted
Run your application
Start the development server:Open your browser to http://localhost:3000.
CheckpointYou should now have a fully functional Auth0 login flow. When you:
- Click Login — you’re redirected to Auth0’s Universal Login page
- Complete authentication — you’re redirected back to your app at
/auth/callback - Visit
/profile— you see your user information - Click Logout — your session is cleared and you’re logged out of Auth0
Advanced Usage
Calling a protected API with an access token
Calling a protected API with an access token
Configure the SDK with an Retrieve the token in a protected route:The SDK handles token refresh automatically when the access token expires.
audience to request an access token for your API, then retrieve it with getAccessToken().Add your API audience to .env:.env
server.js
Using custom login with returnTo
Using custom login with returnTo
Redirect users back to a specific page after login using the
returnTo parameter:server.js
Custom authorization middleware
Custom authorization middleware
Troubleshooting
'req.auth0 is undefined'
'req.auth0 is undefined'
Cause:
createAuth0() middleware was not registered before your route handler.Fix: Ensure app.use(createAuth0()) appears before any route that accesses req.auth0:Callback URL mismatch error
Callback URL mismatch error
Cause: The callback URL in your Auth0 Application Settings does not match
http://localhost:3000/auth/callback.Fix:- Go to Auth0 Dashboard → Applications > Applications → your app → Application Settings
- Add
http://localhost:3000/auth/callbackto Allowed Callback URLs - Add
http://localhost:3000to Allowed Logout URLs - Click Save Changes
@auth0/auth0-express SDK uses /auth/callback (not /callback as in express-openid-connect).Environment variables not loaded
Environment variables not loaded
Cause:
dotenv/config is not imported, or the .env file is missing required values.Fix:- Ensure
import 'dotenv/config'(orrequire('dotenv').config()) is at the top of your entry file - Verify your
.envcontains all five required variables:AUTH0_DOMAIN,AUTH0_CLIENT_ID,AUTH0_CLIENT_SECRET,APP_BASE_URL,AUTH0_SESSION_SECRET - Debug missing values:
'Invalid state' error after login
'Invalid state' error after login
Cause: Session cookie is not being set correctly, or the callback URL is accessed directly.Fix:
- Ensure
APP_BASE_URLmatches the URL you access in your browser (e.g.,http://localhost:3000) - Clear your browser cookies and try again
- In production, ensure you are using HTTPS
Next Steps
- Protect an Express API — Use
@auth0/auth0-express-apito validate access tokens in your API - Add Authorization — Implement role-based access control
- Customize Universal Login — Brand your login experience
- Add Social Connections — Enable Google, GitHub, and other social logins
- Implement MFA — Add multi-factor authentication
Resources
- auth0/auth0-express GitHub — Source code and examples
- Auth0 Community — Get help from the community