Skip to main content

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, add the auth0_flutter SDK dependency, configure web/index.html, set up callback URLs, and implement login/logout flows. Full agent skills documentation →
Prerequisites: Before you begin, ensure you have the following installed:Verify installation: flutter --versionIf you don’t have a Flutter web app, create one: flutter create --platforms=web my_app

Get Started

This quickstart demonstrates how to add Auth0 authentication to a Flutter Web application. You’ll build a secure single-page app with login, logout, and user profile features using the Auth0 Flutter SDK.
1

Create a new project

Create a new Flutter Web application for this quickstart:
Open the project:
2

Install the Auth0 Flutter SDK

Add the Auth0 Flutter SDK to your project using the Flutter CLI:
The SDK requires the Auth0 SPA JS library to be loaded in your web application. Add the following <script> tag to your web/index.html file, before the closing </body> tag:
web/index.html
The Auth0 SPA JS script is required for the Flutter Web SDK to function. Without it, authentication will not work.
3

Setup your Auth0 App

Next, you need to create a new application on your Auth0 tenant.You can choose to set up your Auth0 app automatically by running a CLI command, or do it manually via the Dashboard:
Run the following command in your project’s root directory to create an Auth0 application:
This command will:
  1. Check if you’re authenticated (and prompt for login if needed)
  2. Create an Auth0 Single Page Application configured for http://localhost:3000
  3. Generate lib/auth_config.dart with your Auth0 domain and client ID as Dart constants
4

Configure the SDK

Create an instance of the Auth0Web class with your Auth0 domain and client ID values.Create a new file lib/auth0_service.dart:
lib/auth0_service.dart
Replace YOUR_AUTH0_DOMAIN with your Auth0 tenant domain (e.g., dev-abc123.us.auth0.com) and YOUR_AUTH0_CLIENT_ID with your application’s Client ID from the dashboard.
Setting cacheLocation: CacheLocation.localStorage enables persistent sessions across page reloads.
5

Create the main view

Replace the contents of lib/main.dart with the following code:
lib/main.dart
Key points:
  • onLoad() is called in initState() to handle the authentication callback
  • loginWithRedirect() redirects users to Auth0’s Universal Login page
  • logout() clears the session and redirects back to your app
  • User profile information is accessed via credentials.user
6

Run your app

Run your Flutter Web application on port 3000:
Flutter 3.24.0 and above supports WASM compilation for improved performance:
CheckpointYou should now have a fully functional Auth0 login page running on http://localhost:3000. When you:
  1. Click “Log In” - you’re redirected to Auth0’s Universal Login page
  2. Complete authentication - you’re redirected back to your app
  3. Click “View Profile” - you see your user information
  4. Click “Log Out” - you’re logged out of both your app and Auth0

Advanced Usage

Configure the SDK to request an access token for calling protected APIs:
lib/auth0_service.dart
Use the access token to call your API:
Pass additional parameters to the login flow:
Implement proper error handling for authentication failures:
Check if the user is already authenticated without showing the login page:

Troubleshooting

”Callback URL mismatch” error

Problem: The callback URL doesn’t match what’s configured in Auth0.Solution: Ensure the callback URL in your code matches exactly what’s in the Auth0 Dashboard:
  1. Go to Auth0 Dashboard → Applications → Your App → Settings
  2. Verify Allowed Callback URLs includes http://localhost:3000
  3. The URL must match exactly (no trailing slashes unless you include them in code)

Authentication not working

Problem: Login button does nothing or authentication fails.Solution: Verify the Auth0 SPA JS script is loaded in web/index.html:
This script must be present before the closing </body> tag.

User logged out after page refresh

Problem: User session doesn’t persist across page reloads.Solutions:
  1. Ensure Allowed Web Origins includes http://localhost:3000 in Auth0 Dashboard
  2. Use cacheLocation: CacheLocation.localStorage when creating Auth0Web instance
  3. Verify onLoad() is called in your widget’s initState()

”Invalid state” error

Problem: State mismatch during authentication callback.Solutions:
  1. Clear browser cache and local storage
  2. Ensure you’re not opening multiple tabs during login
  3. Verify your callback URL is correct

CORS errors in browser console

Problem: Cross-Origin Resource Sharing errors.Solution:
  1. Add http://localhost:3000 to Allowed Web Origins in Auth0 Dashboard
  2. Ensure you’re running on port 3000 (matching your configuration)

Next Steps

Now that you have authentication working, consider exploring:

Resources