By Evan Sims
Auth0’s Laravel SDK allows you to quickly add authentication, user profile management, and routing 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.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:
Login Routes
The SDK automatically registers all the necessary routes for your application’s users to authenticate.| Route | Purpose |
|---|---|
/login | Initiates the authentication flow. |
/logout | Logs the user out. |
/callback | Handles the callback from Auth0. |
Access Control
Laravel’s authentication facilities use “guards” to define how users are authenticated for each request. You can use the Auth0 SDK’s authentication guard to restrict access to your application’s routes. To require users to authenticate before accessing a route, you can use Laravel’sauth middleware:
can middleware:
User Information
Information about the authenticated user is available through Laravel’sAuth Facade, or the auth() helper function.
For example, to retrieve the user’s identifier and email address:
User Management
You can update user information using the Auth0 Management API. All Management endpoints are 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, in which we will update a user’s metadata and assign a random favorite color, you should grant the read:users and update:users scopes. A list of API endpoints and the required scopes can be found in the Management API documentation.
Run the Application
You are now ready to start your Laravel application, so it can accept requests:Checkpoint
Open your web browser and try accessing the following routes:http://localhost:8000to see the public route.http://localhost:8000/privateto be prompted to authenticate.http://localhost:8000to see the pubic route, now authenticated.http://localhost:8000/scopeto check if you have theread:messagespermission.http://localhost:8000/updateto update the user’s profile.http://localhost:8000/logoutto log out.
- Try running
php artisan optimize:clearto clear Laravel’s cache. - Ensure your
.auth0.app.jsonand.auth0.api.jsonfiles 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 APIfrom 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.