Authorization
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.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:.gitignore
file:api
middleware, which by default Laravel applies to all routes in your application’s routes/api.php
file.routes/api.php
file.Authorization
header, you can use Laravel’s auth
middleware:can
middleware:Auth
Facade, or the auth()
helper function.For example, to retrieve the user’s identifier and email address: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, you should grant the read:users
scope. A list of API endpoints and the required scopes can be found in the Management API documentation./me
route we created above will not work with a test token as there is no actual user associated with it.curl --request GET \ --url http://localhost:8000/api \ --header 'Accept: application/json'
Next, use your access token in an Authorization
header to request a protected route:curl --request GET \ --url http://localhost:8000/api/private \ --header 'Accept: application/json' \ --header 'Authorization: Bearer YOUR_ACCESS_TOKEN'
Finally, try requesting the scope-protected route, which will only succeed if the access token has the read:messages
scope granted:curl --request GET \ --url http://localhost:8000/api/scope \ --header 'Accept: application/json' \ --header 'Authorization: Bearer YOUR_ACCESS_TOKEN'
cd
into the new project directory:.gitignore
file:api
middleware, which by default Laravel applies to all routes in your application’s routes/api.php
file.routes/api.php
file.Authorization
header, you can use Laravel’s auth
middleware:can
middleware:Auth
Facade, or the auth()
helper function.For example, to retrieve the user’s identifier and email address: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, you should grant the read:users
scope. A list of API endpoints and the required scopes can be found in the Management API documentation./me
route we created above will not work with a test token as there is no actual user associated with it.curl --request GET \ --url http://localhost:8000/api \ --header 'Accept: application/json'
Next, use your access token in an Authorization
header to request a protected route:curl --request GET \ --url http://localhost:8000/api/private \ --header 'Accept: application/json' \ --header 'Authorization: Bearer YOUR_ACCESS_TOKEN'
Finally, try requesting the scope-protected route, which will only succeed if the access token has the read:messages
scope granted:curl --request GET \ --url http://localhost:8000/api/scope \ --header 'Accept: application/json' \ --header 'Authorization: Bearer YOUR_ACCESS_TOKEN'