> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Login to Your Laravel Application

> This guide demonstrates how to integrate Auth0 with a new or existing Laravel application using the Auth0 Laravel SDK.

[View On GitHub](https://github.com/auth0-samples/laravel/tree/7.x/sample)

Download Sample

> [accordion] 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](https://agentskills.io/home).
>
> **Install:**
>
> ```bash theme={null}
> npx skills add auth0/agent-skills --skill auth0
> ```
>
> **Then ask your AI assistant:**
>
> ```text theme={null}
> Add Auth0 authentication to my Laravel app
> ```
>
> 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 →](/docs/docs/quickstart/agent-skills)

> [!NOTE]
>
> **Prerequisites:** Before you begin, ensure you have the following installed:
>
> * **[PHP](https://www.php.net/downloads.php)** 8.2 or newer
> * **[Composer](https://getcomposer.org/download/)** 2.x
> * **[Laravel](https://laravel.com/docs/installation)** 11, 12, or 13
>
> Verify installation: `php --version && composer --version`

## 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](https://github.com/auth0/laravel-auth0).

**1. Create a new Laravel project**

If you already have a Laravel application, skip to Step 2.

Create a new Laravel project:

```bash theme={null}
composer create-project laravel/laravel auth0-laravel-app
```

Open the project directory:

```bash theme={null}
cd auth0-laravel-app
```

**2. Install the Auth0 Laravel SDK**

Run the following command in your project directory to install the [Auth0 Laravel SDK](https://github.com/auth0/laravel-auth0):

```bash theme={null}
composer require auth0/login:^7 --update-with-all-dependencies
```

Then publish the SDK configuration file:

```bash theme={null}
php artisan vendor:publish --tag auth0
```

**3. Configure Auth0 credentials**

You need to create an Auth0 application and add your credentials to the project. Choose one of the following methods:

> [tab] Quick Setup (recommended)
>
> 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:

> [tab] CLI
>
> Run the following command in your project's root directory to create an Auth0 app and generate a `.env` file:
>
> ```bash Mac theme={null}
> # Install Auth0 CLI (if not already installed)
> brew tap auth0/auth0-cli && brew install auth0
>
> # Set up Auth0 app and generate .env file
> auth0 qs setup --app --type regular --framework laravel --build-tool composer --name "My Laravel App" --port 8000
> ```
>
> ```powershell Windows theme={null}
> # Install Auth0 CLI (if not already installed)
> scoop bucket add auth0 https://github.com/auth0/scoop-auth0-cli.git
> scoop install auth0
>
> # Set up Auth0 app and generate .env file
> auth0 qs setup --app --type regular --framework laravel --build-tool composer --name "My Laravel App" --port 8000
> ```
>
> > [!NOTE]
> >
> > This command will:
> >
> > 1. Check if you're authenticated (and prompt for login if needed)
> > 2. Create an Auth0 Regular Web Application configured for `http://localhost:8000`
> > 3. Generate a `.env` file with your Auth0 credentials

> [tab] Dashboard
>
> 1. Go to the [Auth0 Dashboard](https://manage.auth0.com/) → **Applications > Applications**
> 2. Click **Create Application**, enter a name, select **Regular Web Application**, and click **Create**
> 3. Go to the **Settings** tab and copy the **Domain**, **Client ID**, and **Client Secret**
>
> Configure the following URLs in the **Settings** tab of your application:
>
> | Field | Value |
> | - | - |
> | Allowed Callback URLs | `http://localhost:8000/callback` |
> | Allowed Logout URLs | `http://localhost:8000` |
>
> Click **Save Changes**.
>
> Then add your credentials to your project's `.env` file:
>
> ```bash .env theme={null}
> AUTH0_DOMAIN=YOUR_AUTH0_DOMAIN
> AUTH0_CLIENT_ID=YOUR_AUTH0_CLIENT_ID
> AUTH0_CLIENT_SECRET=YOUR_AUTH0_CLIENT_SECRET
> ```
>
> > [!WARNING]
> >
> > Never commit your `AUTH0_CLIENT_SECRET` to version control. Keep it in `.env` and ensure `.env` is in your `.gitignore`.

**4. Add authentication routes**

The Auth0 SDK automatically registers the following routes for your application — no additional route configuration is required:

| Route | Purpose |
| - | - |
| `/login` | Initiates the Auth0 login flow |
| `/logout` | Logs out the user and redirects to Auth0 |
| `/callback` | Handles the Auth0 authentication callback |

> [!NOTE]
>
> 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](https://github.com/auth0/laravel-auth0) for instructions on manual route registration.

Update `routes/web.php` to add your home route:

```php routes/web.php lines theme={null}
<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    if (! auth()->check()) {
        return response('You are not logged in. <a href="/login">Log in</a>');
    }

    $user = auth()->user();
    $name = $user->name ?? 'User';
    $email = $user->email ?? '';

    return response("Hello {$name}! Your email address is {$email}.");
});
```

**5. Protect routes with middleware**

Use Laravel's `auth` middleware to require authentication on any route. You can also enforce specific [permissions](/docs/docs/manage-users/access-control/rbac) using the `can` middleware:

```php routes/web.php lines theme={null}
<?php

use Illuminate\Support\Facades\Route;

// Requires any authenticated user
Route::get('/private', function () {
    return response('Welcome! You are logged in.');
})->middleware('auth');

// Requires authentication AND the 'read:messages' permission
Route::get('/scope', function () {
    return response('You have the read:messages permission.');
})->middleware('auth')->can('read:messages');
```

> [!NOTE]
>
> Permissions are defined in your Auth0 API settings and assigned to users via roles. See [Role-Based Access Control](/docs/docs/manage-users/access-control/rbac) for details.

**6. Run your application**

```bash theme={null}
php artisan serve
```

> [!NOTE]
>
> Your application runs at [http://localhost:8000](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.

> [!TIP]
>
> **Checkpoint**
>
> Open [http://localhost:8000](http://localhost:8000) in your browser. Try these routes to verify your integration:
>
> * [http://localhost:8000/login](http://localhost:8000/login) — triggers the Auth0 login flow
> * [http://localhost:8000/private](http://localhost:8000/private) — redirects to login if unauthenticated; shows a welcome message when logged in
> * [http://localhost:8000/logout](http://localhost:8000/logout) — logs out and returns to the home page

***

## Troubleshooting

> [accordion] 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:**
>
> 1. Go to the [Auth0 Dashboard](https://manage.auth0.com/) → **Applications > Applications** → your app → **Settings**
> 2. Add `http://localhost:8000/callback` to **Allowed Callback URLs**
> 3. Click **Save Changes**
>
> Ensure there are no trailing slashes and that the port matches your running server.

> [accordion] 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:**
>
> 1. Ensure your `SESSION_DRIVER` in `.env` is set to `file`, `database`, or `redis` (not `array`)
> 2. Clear the config and cache:
>
> ```bash theme={null}
> php artisan config:clear && php artisan cache:clear
> ```
>
> 3. Restart your development server

> [accordion] 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 `.env` file with `AUTH0_DOMAIN`, `AUTH0_CLIENT_ID`, and `AUTH0_CLIENT_SECRET`
> * A `.auth0.app.json` file generated by the Auth0 CLI
>
> After updating `.env`, clear the config cache:
>
> ```bash theme={null}
> php artisan config:clear
> ```

> [accordion] 404 on /login, /logout, or /callback
>
> **Cause:** The SDK's routes aren't registered, usually because the service provider didn't load.
>
> **Fix:**
>
> 1. Confirm you ran `php artisan vendor:publish --tag auth0`
> 2. Verify package auto-discovery is enabled (check `composer.json` for `"dont-discover": []`)
> 3. Run `php artisan route:list | grep auth0` to confirm the routes are registered
> 4. If routes are missing, manually register the service provider. In Laravel 11+, add it to `bootstrap/providers.php`:
>
> ```php bootstrap/providers.php theme={null}
> return [
>     App\Providers\AppServiceProvider::class,
>     Auth0\Laravel\Auth0ServiceProvider::class,
> ];
> ```

***

## Advanced Usage

> [accordion] Management API — update user metadata
>
> You can update user information using the [Auth0 Management API](https://github.com/auth0/laravel-auth0/blob/main/docs/Management.md). All Management API endpoints are accessible via the SDK's `Auth0::management()` method.
>
> **Before making Management API calls**, authorize your application to access the Management API:
>
> 1. Go to the [Auth0 Dashboard](https://manage.auth0.com/) → **Applications > APIs** → **Auth0 Management API**
> 2. Select the **Machine to Machine Applications** tab
> 3. Authorize your Laravel application and grant the `read:users` and `update:users` scopes
>
> ```php routes/web.php lines theme={null}
> <?php
>
> use Auth0\Laravel\Facade\Auth0;
> use Illuminate\Support\Facades\Route;
>
> Route::get('/colors', function () {
>     $colors = ['red', 'blue', 'green', 'black', 'white', 'yellow', 'purple', 'orange'];
>
>     $users = Auth0::management()->users();
>
>     // Update the authenticated user's metadata with a random favorite color
>     $users->update(
>         id: auth()->id(),
>         body: [
>             'user_metadata' => [
>                 'color' => $colors[random_int(0, count($colors) - 1)],
>             ],
>         ]
>     );
>
>     // Retrieve and display the updated metadata
>     $metadata = Auth0::json($users->get(auth()->id()));
>     $color = $metadata['user_metadata']['color'] ?? 'unknown';
>     $name = auth()->user()->name;
>
>     return response("Hello {$name}! Your favorite color is {$color}.");
> })->middleware('auth');
> ```
>
> A full reference of all Management API methods is available in the [SDK documentation](https://github.com/auth0/laravel-auth0/blob/main/docs/Management.md).

> [accordion] 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](https://github.com/auth0/laravel-auth0/blob/main/docs/Users.md) for the full implementation guide.

> [accordion] 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](https://github.com/auth0/laravel-auth0/blob/main/docs/Events.md) for a full list of events and implementation examples.

> [accordion] Role-Based Access Control (RBAC)
>
> Use Auth0 RBAC to assign permissions to users via roles, then enforce them in Laravel using the `can` middleware:
>
> ```php routes/web.php theme={null}
> <?php
>
> use Illuminate\Support\Facades\Route;
>
> Route::get('/admin', function () {
>     return response('Admin area.');
> })->middleware('auth')->can('admin:dashboard');
> ```
>
> To set up RBAC:
>
> 1. Go to the [Auth0 Dashboard](https://manage.auth0.com/) → **Applications > APIs** → your API → **Permissions**
> 2. Add the permissions your application needs (e.g., `admin:dashboard`, `read:messages`)
> 3. Go to **User Management > Roles**, create a role, and assign permissions to it
> 4. Assign the role to users from their profile page
>
> See [Role-Based Access Control](/docs/docs/manage-users/access-control/rbac) for the full guide.

***

## Next Steps

Now that you have authentication working in your Laravel application, explore more Auth0 features:

* [Auth0 Dashboard](https://manage.auth0.com/) — Configure and manage your Auth0 tenant and applications
* [laravel-auth0 SDK](https://github.com/auth0/laravel-auth0) — Full SDK documentation and advanced integrations
* [Role-Based Access Control](/docs/docs/manage-users/access-control/rbac) — Assign permissions to users with roles
* [Auth0 Marketplace](https://marketplace.auth0.com/) — Discover integrations to extend Auth0's functionality
