Get Started
This quickstart demonstrates how to add Auth0 authentication to a Flask application. You’ll build a secure web application with login functionality, protected routes, and user profile access using the Auth0 WebApp Python SDK.Install dependencies
Create The
requirements.txt to track your dependencies:requirements.txt
requirements.txt file tracks all project dependencies. To install them, run:Setup your Auth0 App
Next up, you need to create a new app on your Auth0 tenant and add the environment variables to your project.You can choose to do this automatically by running a CLI command or do it manually via the Dashboard:
- CLI
- Dashboard
Run the following shell command on your project’s root directory to create an Auth0 application and generate a
.env file:Create Auth Configuration, Routes, and Templates
Create filesAnd add the following code snippets:
Development Only: This example uses simple in-memory storage classes (
MemoryStateStore and MemoryTransactionStore) for demonstration purposes. These stores will lose all session data when your application restarts and won’t work in multi-instance deployments.For production applications, you must implement persistent storage. The SDK is framework-agnostic and requires you to provide custom StateStore and TransactionStore implementations. See the official SDK storage examples for detailed guidance on implementing Redis, PostgreSQL, or other persistent storage backends.Run your app
Start the Flask development server:Your app will be available at http://localhost:5000. The Auth0 SDK handles authentication routes automatically.
CheckpointYou should now have a fully functional Auth0 login page running on your localhost
Advanced Usage
Accessing API Tokens
Accessing API Tokens
If you need to call a protected API, retrieve an access token:
To use this feature, you must:
- Set
AUTH0_AUDIENCEin your.envfile - Include
offline_accessin your scopes (for refresh tokens) - Update
authorization_paramsinauth.py:
Custom Storage Configuration
Custom Storage Configuration
By default, the SDK uses cookie-based storage. For production environments with specific needs (horizontal scaling, session sharing across services), you can configure custom storage backends like Redis or PostgreSQL.When to use custom storage:
- You need to share sessions across multiple servers
- You have large session data exceeding cookie size limits
- You need centralized session management for backchannel logout
auth.py
For most applications, the default cookie-based storage is sufficient. Custom storage requires implementing the
StateStore interface. See the SDK examples for detailed implementations.Common Issues
MissingRequiredArgumentError
MissingRequiredArgumentError
Problem: You see “MissingRequiredArgumentError: secret” when starting the appCause: The
AUTH0_SECRET environment variable is missing or not loaded properly.Solution:- Verify your
.envfile exists in the project root - Ensure
python-dotenvis installed:pip install python-dotenv - Generate a new secret if needed:
openssl rand -hex 64 - Add it to
.env:AUTH0_SECRET=your_generated_secret - Restart your Flask application
Invalid Callback URL Error
Invalid Callback URL Error
Problem: You see “Callback URL mismatch” or “invalid_request” error during loginCause: The redirect URI in your code doesn’t match what’s registered in Auth0 Dashboard.Solution:
- Check your
.envfile:AUTH0_REDIRECT_URI=http://localhost:5000/callback - Go to Auth0 Dashboard → Applications → Your App → Settings
- Add
http://localhost:5000/callbackto Allowed Callback URLs - Click Save Changes
- Restart your Flask application
AsyncIO Event Loop Errors
AsyncIO Event Loop Errors
Problem: You see “RuntimeError: This event loop is already running” or similar async errorsCause: Flask 2.0+ async support may have issues with certain configurations.Solution:Install Flask with async support:Then restart your Flask application.
Module Not Found Errors
Module Not Found Errors
Problem: You see “ModuleNotFoundError: No module named ‘auth0_server_python’” or similarCause: The SDK is not installed or the virtual environment is not activated.Solution:
- Ensure your virtual environment is activated:
- Install the SDK:
- Verify installation:
ClaimDecodingFailed Error
ClaimDecodingFailed Error
Problem: You see “ClaimDecodingFailed” or “Failed to decode claims” error during authenticationCause: The ID token or access token received from Auth0 couldn’t be properly decoded, often due to:
- Invalid JWT format
- Corrupted session data
- Mismatched signing algorithms
- Clock skew between your server and Auth0
- Ensure your
AUTH0_CLIENT_SECRETis correct in the.envfile - Check your system time is synchronized (NTP):
- Clear browser cookies and restart authentication
- Verify the
AUTH0_DOMAINdoesn’t includehttps://prefix - Check Auth0 Dashboard → Applications → Your App → Settings → Advanced → OAuth → JsonWebToken Signature Algorithm matches your SDK configuration
Token Expired or Invalid
Token Expired or Invalid
Problem: You see “Token has expired” or “invalid_token” errorsCause: The access token or ID token has exceeded its lifetime, or the session has expired.Solution:
- The SDK automatically handles token refresh if you include
offline_accessscope: - For APIs, ensure you’re requesting fresh tokens:
- Adjust token lifetimes in Auth0 Dashboard → Applications → Your App → Settings → Advanced → OAuth
- Implement proper error handling to redirect users to login when tokens expire
CORS and Cross-Origin Errors
CORS and Cross-Origin Errors
Problem: You see CORS errors or “Blocked by CORS policy” in browser consoleCause: Your application’s origin isn’t properly configured in Auth0.Solution:
- Add your origin to Auth0 Dashboard → Applications → Your App → Settings:
- Allowed Web Origins:
http://localhost:5000 - Allowed Callback URLs:
http://localhost:5000/callback - Allowed Logout URLs:
http://localhost:5000
- Allowed Web Origins:
- For production, add your production URLs:
- Ensure your Flask app has proper CORS configuration if calling Auth0 APIs from frontend JavaScript:
Rate Limit Exceeded
Rate Limit Exceeded
Problem: You see “Too many requests” or “Rate limit exceeded” errorsCause: Your application has exceeded Auth0’s rate limits for authentication requests.Solution:
- Check Auth0 Dashboard → Monitoring → Logs for rate limit details
- Implement exponential backoff for retries:
- Review your Auth0 subscription plan limits
- Optimize authentication flow to reduce unnecessary token requests
- Cache tokens appropriately instead of requesting new ones frequently