In this tutorial you will learn about the Laravel 8 REST API with Passport Authentication Tutorial and its application with practical example.
In this Laravel 8 REST API with Passport Authentication Tutorial I will show you how to create REST API with passport authentication In laravel. In this tutorial you will learn to create rest api for crud operation with passport authentication In laravel 8 application. In this article I will share example to create rest api with passport authentication in laravel. I will also show you how to install passport auth package in laravel. After installing and configure passport authentication in laravel we will create simple crud operation rest api. In this article we will be creating a post management crud operation rest api with passport authentication in laravel.
Laravel 8 REST API with Passport Authentication Tutorial
In this step by step tutorial I will guide you through create a fully functional restful API with passport authentication in Laravel 8. We will be creating fully functional REST API along with passport Authentication. Please follow the instruction given below:
- Install New Laravel Project
- Set Up Database
- Install Passport Package
- Configure Passport Module
- Create Post Model & Run Migration
- Create a New Controller
- Define API Routes
- Test Laravel Passport API
Install New Laravel Project
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel laravel-passport-auth --prefer-dist |
Set Up Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= |
Install Passport Package
In this step, we need to install Laravel Passport package via the composer dependency manager. Use the following command to install passport package.
1 |
composer require laravel/passport |
Now, run following command to migrate database schema.
1 |
php artisan migrate |
Now, it is mandatory to install passport using the command below. This command will generate encryption keys required to generate secret access tokens.
1 |
php artisan passport:install |
Configure Passport Module
Open App/User.php model file and add ‘Laravel\Passport\HasApiTokens’ trait in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Passport\HasApiTokens; class User extends Authenticatable { use HasFactory, Notifiable, HasApiTokens; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; } |
Next, open app/Providers/AuthServiceProvider.php file and register the registerPolicies() method inside the boot() function, It will evoke the required routes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace App\Providers; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Gate; use Laravel\Passport\Passport; class AuthServiceProvider extends ServiceProvider { /** * The policy mappings for the application. * * @var array */ protected $policies = [ 'App\Models\Model' => 'App\Policies\ModelPolicy', ]; /** * Register any authentication / authorization services. * * @return void */ public function boot() { $this->registerPolicies(); Passport::routes(); } } |
Register the PassportServiceProvider class in providers array inside the config/app.php file:
1 2 3 4 5 6 7 |
'providers' => [ ... ... ... Laravel\Passport\PassportServiceProvider::class, ], |
Configure driver for the Passport, get inside the config/auth.php file and make the changes as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php return [ .... .... 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'passport', 'provider' => 'users', ], ], .... .... ] |
Create Posts Model & Run Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Post -m |
Once above command is executed there will be a migration file created inside database/migrations/ directory, just open timestamp_create_posts_table.php migration file and update the function up() method as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->unsignedBigInteger('user_id'); $table->text('title'); $table->longText('description'); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } } |
Next, go to app/Models/Post.php file and add the $fillable property in the Post model as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; protected $fillable = [ 'title', 'description' ]; } |
Now, run the migration to create database table using following artisan command: