Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/auth0/laravel-auth0

Laravel SDK for Auth0 Authentication and Management APIs.
https://github.com/auth0/laravel-auth0

authentication dx-sdk laravel management php

Last synced: 5 days ago
JSON representation

Laravel SDK for Auth0 Authentication and Management APIs.

Awesome Lists containing this project

README

        

![Auth0 Laravel SDK](https://cdn.auth0.com/website/sdks/banners/laravel-auth0-banner.png)



Build Status
Code Coverage
Total Downloads
License


**The Auth0 Laravel SDK is a PHP package that integrates [Auth0](https://auth0.com) into your Laravel application.** It includes no-code user authentication, extensive Management API support, permissions-based routing access control, and more.

- [Requirements](#requirements)
- [Getting Started](#getting-started)
- [1. Install the SDK](#1-install-the-sdk)
- [2. Install the CLI](#2-install-the-cli)
- [3. Configure the SDK](#3-configure-the-sdk)
- [4. Run the Application](#4-run-the-application)
- [Documentation](#documentation)
- [QuickStarts](#quickstarts)
- [Contributing](#contributing)
- [Code of Conduct](#code-of-conduct)
- [Security](#security)
- [License](#license)

## Requirements

Your application must use a [supported Laravel version](#supported-laravel-releases), and your host environment must be running a [maintained PHP version](https://www.php.net/supported-versions.php). Please review [our support policy](./docs/Support.md) for more information.

You will also need [Composer](https://getcomposer.org/) and an [Auth0 account](https://auth0.com/signup).

### Supported Laravel Releases

The next major release of Laravel is forecasted for Q1 2025. We anticipate supporting it upon release.

| Laravel | SDK | PHP | Supported Until |
| ---------------------------------------------- | ----- | ---------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| [11.x](https://laravel.com/docs/11.x/releases) | 7.13+ | [8.3](https://www.php.net/releases/8.3/en.php) | Approx. [March 2026](https://laravel.com/docs/11.x/releases#support-policy) (EOL for Laravel 11) |
| | | [8.2](https://www.php.net/releases/8.2/en.php) | Approx. [Dec 2025](https://www.php.net/supported-versions.php) (EOL for PHP 8.2) |

We strive to support all actively maintained Laravel releases, prioritizing support for the latest major version with our SDK. If a new Laravel major introduces breaking changes, we may have to end support for past Laravel versions earlier than planned.

Affected Laravel versions will still receive security fixes until their end-of-life date, as announced in our release notes.

### Maintenance Releases

The following releases are no longer being updated with new features by Auth0, but will continue to receive security updates through their end-of-life date.

| Laravel | SDK | PHP | Security Fixes Until |
| ---------------------------------------------- | ---------- | ---------------------------------------------- | -------------------------------------------------------------------------------------- |
| [10.x](https://laravel.com/docs/10.x/releases) | 7.5 - 7.12 | [8.3](https://www.php.net/releases/8.3/en.php) | [Feb 2025](https://laravel.com/docs/10.x/releases#support-policy) (EOL for Laravel 10) |
| | | [8.2](https://www.php.net/releases/8.2/en.php) | [Feb 2025](https://laravel.com/docs/10.x/releases#support-policy) (EOL for Laravel 10) |
| | | [8.1](https://www.php.net/releases/8.2/en.php) | [Nov 2024](https://www.php.net/supported-versions.php) (EOL for PHP 8.1) |

### Unsupported Releases

The following releases are unsupported by Auth0. While they may be suitable for some legacy applications, your mileage may vary. We recommend upgrading to a supported version as soon as possible.

| Laravel | SDK |
| -------------------------------------------- | ---------- |
| [9.x](https://laravel.com/docs/9.x/releases) | 7.0 - 7.12 |
| [8.x](https://laravel.com/docs/8.x/releases) | 7.0 - 7.4 |
| [7.x](https://laravel.com/docs/7.x/releases) | 5.4 - 6.5 |
| [6.x](https://laravel.com/docs/6.x/releases) | 5.3 - 6.5 |
| [5.x](https://laravel.com/docs/5.x/releases) | 2.0 - 6.1 |
| [4.x](https://laravel.com/docs/4.x/releases) | 1.x |

## Getting Started

The following is our recommended approach to getting started with the SDK. Alternatives are available in [our expanded installation guide](./docs/Installation.md).

### 1. Install the SDK

- For **new applications**, we offer a quickstart template — a version of the default Laravel 9 starter project pre-configured for use with the Auth0 SDK.

```shell
composer create-project auth0-samples/laravel auth0-laravel-app && cd auth0-laravel-app
```

- For **existing applications**, you can install the SDK using Composer.

```shell
composer require auth0/login:^7 --update-with-all-dependencies
```

In this case, you will also need to generate an SDK configuration file for your application.

```shell
php artisan vendor:publish --tag auth0
```

### 2. Install the CLI

1. Install the [Auth0 CLI](https://github.com/auth0/auth0-cli) to manage your account from the command line.

```shell
curl -sSfL https://raw.githubusercontent.com/auth0/auth0-cli/main/install.sh | sh -s -- -b .
```

Move the CLI to a directory in your `PATH` to make it available system-wide.

```shell
sudo mv ./auth0 /usr/local/bin
```

💡 If you prefer not to move the CLI, simply substitute `auth0` in the CLI steps below with `./auth0`.


Using Homebrew (macOS)

```shell
brew tap auth0/auth0-cli && brew install auth0
```


Using Scoop (Windows)

```cmd
scoop bucket add auth0 https://github.com/auth0/scoop-auth0-cli.git
scoop install auth0
```

2. Authenticate the CLI with your Auth0 account. Choose "as a user" if prompted.

```shell
auth0 login
```

### 3. Configure the SDK

1. Register a new application with Auth0.

```shell
auth0 apps create \
--name "My Laravel Application" \
--type "regular" \
--auth-method "post" \
--callbacks "http://localhost:8000/callback" \
--logout-urls "http://localhost:8000" \
--reveal-secrets \
--no-input \
--json > .auth0.app.json
```

2. Register a new API with Auth0.

```shell
auth0 apis create \
--name "My Laravel Application API" \
--identifier "https://github.com/auth0/laravel-auth0" \
--offline-access \
--no-input \
--json > .auth0.api.json
```

3. Add the new files to `.gitignore`.

```bash
echo ".auth0.*.json" >> .gitignore
```


Using Windows PowerShell

```powershell
Add-Content .gitignore "`n.auth0.*.json"
```


Using Windows Command Prompt

```cmd
echo .auth0.*.json >> .gitignore
```

### 4. Run the Application

Boot the application using PHP's built-in web server.

```shell
php artisan serve
```

Direct your browser to [http://localhost:8000](http://localhost:8000) to experiment with the application.

- **Authentication**
Users can log in or out of the application by visiting the [`/login`](http://localhost:8000/login) or [`/logout`](http://localhost:8000/logout) routes, respectively.

- **API Authorization**
For simplicity sake, generate a test token using the CLI.

```shell
auth0 test token \
--audience %IDENTIFIER% \
--scopes "read:messages"
```

Substitute %IDENTIFIER% with the identifier of the API you created in step 3 above.

Now you can send requests to the `/api` endpoints of the application, including the token as a header.

```shell
curl --request GET \
--url http://localhost:8000/api/example \
--header 'Accept: application/json' \
--header 'Authorization: Bearer %TOKEN%'
```

Substitute %TOKEN% with the test token returned in the previous step.


Using Windows PowerShell

```powershell
Invoke-WebRequest http://localhost:8000/api/example `
-Headers @{'Accept' = 'application/json'; 'Authorization' = 'Bearer %TOKEN%'}
```

When you're ready to deploy your application to production, review [our deployment guide](./docs/Deployment.md) for best practices and advice on securing Laravel.

## Integration Examples

User Authentication

The SDK automatically registers all the necessary routes and authentication services within the `web` middleware group of your application to enable users to authenticate without requiring you to write any code.

| Route | Purpose |
| ----------- | ---------------------------------- |
| `/login` | Initiates the authentication flow. |
| `/logout` | Logs the user out. |
| `/callback` | Handles the callback from Auth0. |

If these routes conflict with your application architecture, you can override this default behavior by [adjusting the SDK configuration](./docs/Configuration.md#route-registration).

---

Route Authorization (Access Control)

The SDK automatically registers its authentication and authorization guards within the `web` and `api` middleware groups for your Laravel application, respectively.

For `web` routes, you can use Laravel's `auth` middleware to require that a user be authenticated to access a route.

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

For `api` routes, you can use Laravel's `auth` middleware to require that a request be authenticated with a valid bearer token to access a route.

```php
Route::get('/api/private', function () {
return response()->json(['message' => 'Hello! You included a valid token with your request.']);
})->middleware('auth');
```

In addition to requiring that a user be authenticated, you can also require that the user have specific permissions to access a route, using Laravel's `can` middleware.

```php
Route::get('/scope', function () {
return response('You have the `read:messages` permission, and can therefore access this resource.');
})->middleware('auth')->can('read:messages');
```

Permissions require that [RBAC](https://auth0.com/docs/manage-users/access-control/rbac) be enabled within [your API settings](https://manage.auth0.com/#/apis).

---

Users and Tokens

Laravel's `Auth` Facade can be used to retrieve information about the authenticated user or token associated with a request.

For routes using the `web` middleware group in `routes/web.php`.

```php
Route::get('/', function () {
if (! auth()->check()) {
return response('You are not logged in.');
}

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

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

For routes using the `api` middleware group in `routes/api.php`.

```php
Route::get('/', function () {
if (! auth()->check()) {
return response()->json([
'message' => 'You did not provide a token.',
]);
}

return response()->json([
'message' => 'Your token is valid; you are authorized.',
'id' => auth()->id(),
'token' => auth()?->user()?->getAttributes(),
]);
});
```

---

Management API Calls

Once you've [authorized your application to make Management API calls](./docs/Management.md#api-application-authorization), you'll be able to engage nearly any of the [Auth0 Management API endpoints](https://auth0.com/docs/api/management/v2) through the SDK.

Each API endpoint has its own SDK class which can be accessed through the Facade's `management()` factory method. For interoperability, network responses from the API are returned as [PSR-7 messages](https://www.php-fig.org/psr/psr-7/). These can be converted into native arrays using the SDK's `json()` method.

For example, to update a user's metadata, you can call the `management()->users()->update()` method.

```php
use Auth0\Laravel\Facade\Auth0;

Route::get('/colors', function () {
$colors = ['red', 'blue', 'green', 'black', 'white', 'yellow', 'purple', 'orange', 'pink', 'brown'];

// Update the authenticated user with a randomly assigned favorite color.
Auth0::management()->users()->update(
id: auth()->id(),
body: [
'user_metadata' => [
'color' => $colors[random_int(0, count($colors) - 1)]
]
]
);

// Retrieve the user's updated profile.
$profile = Auth0::management()->users()->get(auth()->id());

// Convert the PSR-7 response into a native array.
$profile = Auth0::json($profile);

// Extract some values from the user's profile.
$color = $profile['user_metadata']['color'] ?? 'unknown';
$name = auth()->user()->name;

return response("Hello {$name}! Your favorite color is {$color}.");
})->middleware('auth');
```

All the SDK's Management API methods are [documented here](./docs/Management.md).

## Documentation

- [Installation](./docs/Installation.md) — Installing the SDK and generating configuration files.
- [Configuration](./docs/Configuration.md) — Configuring the SDK using JSON files or environment variables.
- [Sessions](./docs/Sessions.md) — Guidance on deciding which Laravel Session API driver to use.
- [Cookies](./docs/Cookies.md) — Important notes about using Laravel's Cookie session driver, and alternative options.
- [Management API](./docs/Management.md) — Using the SDK to work with the [Auth0 Management API](https://auth0.com/docs/api/management/v2).
- [Users](./docs/Users.md) — Extending the SDK to support persistent storage and [Eloquent](https://laravel.com/docs/eloquent) models.
- [Events](./docs/Events.md) — Hooking into SDK [events](https://laravel.com/docs/events) to respond to specific actions.
- [Deployment](./docs/Deployment.md) — Deploying your application to production.

You may find the following integration guidance useful:

- [Laravel Eloquent](./docs/Eloquent.md) — [Eloquent ORM](https://laravel.com/docs/eloquent) is supported.
- [Laravel Octane](./docs/Octane.md) — [Octane](https://laravel.com/docs/octane) is not supported at this time.
- [Laravel Telescope](./docs/Telescope.md) — [Telescope](https://laravel.com/docs/telescope) is compatible as of SDK v7.11.0.

You may also find the following resources helpful:

- [Auth0 Documentation Hub](https://www.auth0.com/docs)
- [Auth0 Management API Explorer](https://auth0.com/docs/api/management/v2)
- [Auth0 Authentication API Explorer](https://auth0.com/docs/api/authentication)

Contributions to improve our documentation [are welcomed](https://github.com/auth0/laravel-auth0/pull).

## QuickStarts

- [Session-based Authentication](https://auth0.com/docs/quickstart/webapp/laravel) ([GitHub](https://github.com/auth0-samples/laravel))
- [Token-based Authorization](https://auth0.com/docs/quickstart/backend/laravel) ([GitHub](https://github.com/auth0-samples/laravel))

## Community

The [Auth0 Community](https://community.auth0.com) is where you can get support, ask questions, and share your projects.

## Contributing

We appreciate feedback and contributions to this library. Before you get started, please review Auth0's [General Contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md).

The [Contribution Guide](./.github/CONTRIBUTING.md) contains information about our development process and expectations, insight into how to propose bug fixes and improvements, and instructions on how to build and test changes to the library.

To provide feedback or report a bug, [please raise an issue](https://github.com/auth0/laravel-auth0/issues).

## Code of Conduct

Participants are expected to adhere to Auth0's [Code of Conduct](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md) when interacting with this project.

## Security

If you believe you have found a security vulnerability, we encourage you to responsibly disclose this and not open a public issue. We will investigate all reports. The [Responsible Disclosure Program](https://auth0.com/whitehat) details the procedure for disclosing security issues.

## License

This library is open-sourced software licensed under the [MIT license](./LICENSE.md).

---





Auth0 Logo

Auth0 is an easy-to-implement, adaptable authentication and authorization platform.
To learn more, check out "Why Auth0?"