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.Test Change
1
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:All the commands in this guide assume you are running them from the root of your Laravel project, directory so
you should
cd
into the new project directory:2
SDK Installation
Run the following command within your project directory to install the Auth0 Laravel SDK:Then generate an SDK configuration file for your application:
3
SDK Configuration
Run the following command from your project directory to download the Auth0 CLI:Then authenticate the CLI with your Auth0 account, choosing “as a user” when prompted:Next, create a new application with Auth0:You should also create a new API:This produces two files in your project directory that configure the SDK.As these files contain credentials it’s important to treat these as sensitive. You should ensure you do not
commit these to version control. If you’re using Git, you should add them to your
.gitignore
file:4
Login Routes
The SDK automatically registers all the necessary routes for your application’s users to authenticate.If you require more control over these, or if they conflict with existing routes in your application, you can
manually register the SDK’s controllers instead. Please see the SDK’s README for advanced integrations.
5
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’s You can also require authenticated users to have specific permissions by combining this
with Laravel’s
auth
middleware.can
middleware.6
User Information
Information about the authenticated user is available through Laravel’s
Auth
Facade, or the
auth()
helper function.For example, to retrieve the user’s identifier and email address:7
User Management
You can update user information using the Auth0 Management API. All Management endpoints are accessible through the SDK’s
A quick reference guide of all the SDK’s Management API methods is available here.
management()
method.Before making Management API calls you must enable your application to communicate with the Management
API. This can be done from the Auth0 Dashboard’s 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.8
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:8000 to see the public route.
- http://localhost:8000/private to be prompted to authenticate.
- http://localhost:8000 to see the public route, now authenticated.
- http://localhost:8000/scope to check if you have the
read:messages
permission. - http://localhost:8000/update to update the user’s profile.
- http://localhost:8000/logout to log out.
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.
Next Steps
Excellent work! If you made it this far, you should now have login, logout, and user profile information running in your application.This concludes our quickstart tutorial, but there is so much more to explore. To learn more about what you can do with Auth0, check out:- Auth0 Dashboard - Learn how to configure and manage your Auth0 tenant and applications
- laravel-auth0 SDK - Explore the SDK used in this tutorial more fully
- Auth0 Marketplace - Discover integrations you can enable to extend Auth0’s functionality