By Evan Sims
Auth0’s Laravel SDK allows you to quickly add token-based authorization and route access control to your Laravel application. This guide demonstrates how to integrate Auth0 with a new or existing Laravel 9 or 10 application.We recommend that you log in to follow this quickstart with examples configured for your account. Backend applications differ from traditional web applications in that they do not handle user authentication or have a user interface. They provide an API that other applications can interact with. They accept access tokens fromAuthorization
headers in requests to control access to routes.
Separate front-end applications are usually built to interact with these types of backends. These can be anything from single-page applications or native or mobile apps (all of which Auth0 also provides SDKs for!)
When users need to interact with your backend application, they first authenticate with Auth0 using the frontend application. The frontend application then retrieves an access token from Auth0, which it can use to make requests to your backend application on behalf of the user.
As their name implies, access tokens are designed to address matters of access control (authorization), and do not contain information about the user. Backend applications work exclusively with access tokens. You can retrieve information about the user who created the token using the Management API, which we will demonstrate later.
Laravel Installation
If you do not already have a Laravel application set up, open a shell to a suitable directory for a new project and run the following command:cd
into the new project directory:
SDK Installation
Run the following command within your project directory to install the Auth0 Laravel SDK:SDK Configuration
Run the following command from your project directory to download the Auth0 CLI:.gitignore
file:
Access Control
You can use the Auth0 SDK’s authorization guard to restrict access to your application’s routes. To reject requests that do not contain a valid access token in theAuthorization
header, you can use Laravel’s auth
middleware:
can
middleware:
Token Information
Information about the provided access token is available through Laravel’sAuth
Facade, or the auth()
helper function.
For example, to retrieve the user’s identifier and email address:
Retrieve User Information
You can retrieve information about the user who created the access token from Auth0 using the Auth0 Management API. The SDK provides a convenient wrapper for this API, accessible through the SDK’smanagement()
method.
Before making Management API calls you must enable your application to communicate with the Management API. This can be done from the Auth0 Dashboards API page, choosing Auth0 Management API
, and selecting the ‘Machine to Machine Applications’ tab. Authorize your Laravel application, and then click the down arrow to choose the scopes you wish to grant.
For the following example, you should grant the read:users
scope. A list of API endpoints and the required scopes can be found in the Management API documentation.
You should cache user information in your application for brief periods. This reduces the number of requests your application makes to Auth0, and improves performance. You should avoid storing user information in your application for long periods as this can lead to stale data. You should also avoid storing user information beyond the user’s identifier in persistent databases.
Run the Application
You are now ready to start your Laravel application, so it can accept requests:Retrieve a Test Token
You can learn more about retrieving access tokens here. For this quickstart, however, you can simply use an access token from your API settings “test” view.The
/me
route we created above will not work with a test token as there is no actual user associated with it.Checkpoint
Open a shell and try issuing requests to your application. Begin by requesting the public route:Authorization
header to request a protected route:
read:messages
scope granted:
- Try running
php artisan optimize:clear
to clear Laravel’s cache. - Ensure your
.auth0.app.json
and.auth0.api.json
files are at the root of your project. - Ensure you have enabled your Laravel application as a Machine-to-Machine application and granted it all the necessary scopes for the
Auth0 Management API
from the Auth0 Dashboard.
Additional Reading
- User Repositories and Models extends the Auth0 Laravel SDK to use custom user models, and how to store and retrieve users from a database.
- Hooking Events covers how to listen for events raised by the Auth0 Laravel SDK, to fully customize the behavior of your integration.
- Management API support is built into the Auth0 Laravel SDK, allowing you to interact with the Management API from your Laravel application.