Use AI to integrate Auth0
Use AI to integrate Auth0
If you use an AI coding assistant like Claude Code, Cursor, or GitHub Copilot, you can add Auth0 authentication automatically in minutes using agent skills.Install:Then ask your AI assistant:Your AI assistant will automatically create your Auth0 application, fetch credentials, install
@auth0/auth0-fastify, configure the plugin, and create all necessary routes and views. Full agent skills documentation →Get Started
This quickstart demonstrates how to add Auth0 authentication to a Fastify application. You’ll build a secure web app with login, logout, and user profile features using the Auth0 Fastify SDK.Create a new project
Create a new directory for your Fastify application and initialize a Node.js project.Initialize the projectCreate the project structure
Install the Auth0 Fastify SDK
Install the required dependenciesUpdate your
We’re using
@fastify/view with ejs for server-side rendering. You can use any template engine supported by Fastify.package.json to add start scripts:package.json
Setup your Auth0 App
Next, you need to create a new application on your Auth0 tenant and add the environment variables to your project.You have three options to set up your Auth0 app: use the Quick Setup tool (recommended), run a CLI command, or configure manually via the Dashboard:
- Quick Setup (recommended)
- CLI
- Dashboard
Create an Auth0 App and copy the pre-filled
.env file with the right configuration values.Configure the Auth0 plugin
Create your Fastify server and register the Auth0 plugin:What this does:
server.js
- Registers the view engine for rendering HTML templates
- Configures the Auth0 plugin with your credentials
- Automatically creates routes at
/auth/login,/auth/logout, and/auth/callback - Handles session management with encrypted cookies
Create view templates
Create a Create the home page template:Create the profile page template:
views directory and add template files:Mac/Linux
Windows
views/home.ejs
views/profile.ejs
Create routes
Add routes to your Key points:
server.js file:server.js
- The home route checks authentication status and passes it to the template
- The profile route uses a
preHandlerto protect the route getSession()returns the user’s session or null if not authenticatedgetUser()returns the authenticated user’s profile information
Run your app
Start the development server:Open your browser to http://localhost:3000.
The
--watch flag in Node.js 20+ automatically restarts the server when files change.CheckpointYou should now have a fully functional Auth0 login page. When you:
- Click “Login” - you’re redirected to Auth0’s Universal Login page
- Complete authentication - you’re redirected back to your app
- Visit “/profile” - you see your user information
- Click “Logout” - you’re logged out of both your app and Auth0
Advanced Usage
Calling Protected APIs with Access Tokens
Calling Protected APIs with Access Tokens
To call external APIs that require an access token, configure the SDK with an audience:Add to your Then retrieve and use the access token:
server.js
.env file:.env
server.js
Custom Route Paths
Custom Route Paths
By default, Auth0 routes are mounted at
/auth/*. You can disable auto-mounting and create custom routes:server.js
Remember to update your Allowed Callback URLs in the Auth0 Dashboard to include your custom callback URL.
Account Linking
Account Linking
Enable users to link multiple authentication providers to a single account:This automatically creates the following routes:
server.js
/auth/connect- Link a new provider/auth/connect/callback- Handle the linking callback/auth/unconnect- Unlink a provider/auth/unconnect/callback- Handle the unlinking callback
views/profile.ejs
Using TypeScript
Using TypeScript
Convert your project to TypeScript for better type safety:Create a Rename Update
tsconfig.json:tsconfig.json
server.js to server.ts and add types:server.ts
package.json:package.json
Troubleshooting
Common Issues and Solutions
Common Issues and Solutions
”Invalid state” error after login
Problem: State mismatch between the authentication request and callback.Solutions:- Ensure cookies are being set correctly (not blocked by browser)
- Verify callback URL matches exactly in Auth0 Dashboard (including
/auth/callback) - Check that
SESSION_SECRETis set and at least 64 characters long
”session is undefined” error
Problem: Unable to retrieve session data.Solution: Ensure the Auth0 plugin is registered before accessing session methods:Callback URL mismatch
Problem: “Callback URL mismatch” error from Auth0.Solution:- Go to your Auth0 Dashboard → Applications → Your App → Settings
- Add
http://localhost:3000/auth/callbackto Allowed Callback URLs - The URL must match exactly (including the
/auth/callbackpath)
Environment variables not loading
Problem: Configuration values areundefined.Solution:- Ensure
import 'dotenv/config'is at the top of your entry file - Verify
.envfile is in the root directory - Check for typos in variable names
Next Steps
Now that you have authentication working, consider exploring:- Fastify API Authentication - Protect your API endpoints with JWT validation
- 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-fastify GitHub - Source code and examples
- Fastify Documentation - Learn more about Fastify
- Auth0 Community - Get help from the community