Home » Laravel » Integrate Passport to your Laravel API’s
Hello everyone, welcome back to justlaravel.com. I am back with another tutorial, here I will show you how to integrate Passport(a Laravel’s API authentication package) into your Laravel applications.
Here I will create 2 Laravel projects, one is an API and other is the API consumer app. It basically means I will create an API with Passport package integrated. A normal Laravel app where I call the API and show its data.
Let’s get started!
First, let us create an API. In the terminal/command prompt run the following command to create a new Laravel project.
laravel new sampleAPI
Passport Install
Now, install the Passport package using composer,
composer require laravel/passport
Now, register the Passport service provider in the providers array of your config/app.php
file.
‘providers’ => [ … … … Laravel\Passport\PassportServiceProvider::class, ],
Laravel\Passport\PassportServiceProvider::class, |
Now, run the following commands to setup passport, but before that setup database details in .env
file ordatabase.php
(app\config\database.php
) file.
The migrate command creates some default Laravel tables and some new tables for authentication which comes from passport.
Passport Integration Laravel – justlaravel.com
Now run,
php artisan passport:install
php artisan passport:install |
it creates some client id’s and secrets, which can be later used by consumer app to access the API.
Passport Configure
Here we need to make some configuration changes to our Laravel app so Passport can be used in the API authentication.
Now, in the User model(app\User.php
) we need to add HasApiTokens
trait from Passport package,