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/login, configure environment variables, and register authentication routes. Full agent skills documentation →Get Started
This quickstart demonstrates how to add Auth0 authentication to a Laravel application. You’ll configure secure login, logout, protected routes, and user profile access using the Auth0 Laravel SDK.Create a new Laravel project
If you already have a Laravel application, skip to Step 2.Create a new Laravel project:Open the project directory:
Install the Auth0 Laravel SDK
Run the following command in your project directory to install the Auth0 Laravel SDK:Then publish the SDK configuration file:
Configure Auth0 credentials
You need to create an Auth0 application and add your credentials to the project. Choose one of the following methods:
- Quick Setup (recommended)
- CLI
- Dashboard
Create an Auth0 App and copy the pre-filled
.env credentials with the right configuration values.Add these values to your project’s .env file:Add authentication routes
The Auth0 SDK automatically registers the following routes for your application — no additional route configuration is required:
Update
| Route | Purpose |
|---|---|
/login | Initiates the Auth0 login flow |
/logout | Logs out the user and redirects to Auth0 |
/callback | Handles the Auth0 authentication callback |
If your application uses Laravel Breeze, Fortify, or Jetstream, the SDK’s
/login, /logout, and /callback routes may conflict with routes registered by those packages. See the SDK README for instructions on manual route registration.routes/web.php to add your home route:routes/web.php
Protect routes with middleware
Use Laravel’s
auth middleware to require authentication on any route. You can also enforce specific permissions using the can middleware:routes/web.php
Permissions are defined in your Auth0 API settings and assigned to users via roles. See Role-Based Access Control for details.
Run your application
Your application runs at http://localhost:8000. If port 8000 is already in use, run
php artisan serve --port=8001 and update your Auth0 application’s Allowed Callback URLs and Allowed Logout URLs to use the new port.CheckpointOpen http://localhost:8000 in your browser. Try these routes to verify your integration:
- http://localhost:8000/login — triggers the Auth0 login flow
- http://localhost:8000/private — redirects to login if unauthenticated; shows a welcome message when logged in
- http://localhost:8000/logout — logs out and returns to the home page
Troubleshooting
Callback URL mismatch error
Callback URL mismatch error
Cause: The callback URL sent to Auth0 doesn’t match any URL in your application’s Allowed Callback URLs list.Fix:
- Go to the Auth0 Dashboard → Applications > Applications → your app → Settings
- Add
http://localhost:8000/callbackto Allowed Callback URLs - Click Save Changes
Invalid state / CSRF error after login
Invalid state / CSRF error after login
Cause: Session or cookie misconfiguration — the Laravel session isn’t persisting state between the login redirect and callback.Fix:
- Ensure your
SESSION_DRIVERin.envis set tofile,database, orredis(notarray) - Clear the config and cache:
- Restart your development server
AUTH0_DOMAIN not set or credentials not loading
AUTH0_DOMAIN not set or credentials not loading
Cause: The SDK cannot find your Auth0 credentials.Fix: Ensure one of the following exists in your project root:
- A
.envfile withAUTH0_DOMAIN,AUTH0_CLIENT_ID, andAUTH0_CLIENT_SECRET - A
.auth0.app.jsonfile generated by the Auth0 CLI
.env, clear the config cache:404 on /login, /logout, or /callback
404 on /login, /logout, or /callback
Cause: The SDK’s routes aren’t registered, usually because the service provider didn’t load.Fix:
- Confirm you ran
php artisan vendor:publish --tag auth0 - Verify package auto-discovery is enabled (check
composer.jsonfor"dont-discover": []) - Run
php artisan route:list | grep auth0to confirm the routes are registered - If routes are missing, manually register the service provider. In Laravel 11+, add it to
bootstrap/providers.php:
bootstrap/providers.php
Advanced Usage
Management API — update user metadata
Management API — update user metadata
You can update user information using the Auth0 Management API. All Management API endpoints are accessible via the SDK’s A full reference of all Management API methods is available in the SDK documentation.
Auth0::management() method.Before making Management API calls, authorize your application to access the Management API:- Go to the Auth0 Dashboard → Applications > APIs → Auth0 Management API
- Select the Machine to Machine Applications tab
- Authorize your Laravel application and grant the
read:usersandupdate:usersscopes
routes/web.php
Custom user models and repositories
Custom user models and repositories
The SDK supports custom user models and repositories, letting you store and retrieve users from your own database while keeping Auth0 as the identity provider.See User Repositories and Models for the full implementation guide.
Listening to SDK events
Listening to SDK events
The SDK raises events at key points in the authentication lifecycle — on login, logout, token refresh, and more — allowing you to fully customize behavior without modifying core SDK code.See Hooking Events for a full list of events and implementation examples.
Role-Based Access Control (RBAC)
Role-Based Access Control (RBAC)
Use Auth0 RBAC to assign permissions to users via roles, then enforce them in Laravel using the To set up RBAC:
can middleware:routes/web.php
- Go to the Auth0 Dashboard → Applications > APIs → your API → Permissions
- Add the permissions your application needs (e.g.,
admin:dashboard,read:messages) - Go to User Management > Roles, create a role, and assign permissions to it
- Assign the role to users from their profile page
Next Steps
Now that you have authentication working in your Laravel application, explore more Auth0 features:- Auth0 Dashboard — Configure and manage your Auth0 tenant and applications
- laravel-auth0 SDK — Full SDK documentation and advanced integrations
- Role-Based Access Control — Assign permissions to users with roles
- Auth0 Marketplace — Discover integrations to extend Auth0’s functionality