Laravel Passport: API Authentication using Token and Scopes
Most systems nowadays require authentication to access the resources. For API we use tokens to authenticate the users and do not maintain the session state between the requests. Laravel provides the package “Passport” for API authentication, which provides the OAuth2 server implementation for the application.
Below are the steps on how to generate the token using Laravel Passport and how to use token scopes:
Step 1: Install Laravel
First of all, we need to install a fresh Laravel application in our system using the below command, So open the terminal and run the below command:
composer create-project --prefer-dist laravel/laravel demo
Step 2: Database Configuration
In this step, we will make/change a database configuration for our project. So let’s open the .env file and fill in all details like as below:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password
Step 3: Install Passport
To install passport, run the following command:
composer require laravel/passport