Laravel 7/6 REST API With Passport Auth Tutorial
Laravel 20-Oct-2020

Laravel 7/6 REST API With Passport Auth Tutorial

Laravel 7/6 RESTful authentication APIs with passport tutorial. Here, you will learn how to create restful login, registration, get user info auth APIs in laravel with passport.

As well as, learn how to install passport auth in your laravel application and configure passport auth with laravel app.

Then, you will learn how to create restful authentication APIs using passport in laravel tutorial.

This laravel 7 passport authentication tutorial will guide on how to install passport & configure passport in laravel app. And implement a restful authentication APIs with passport in laravel app.

Laravel 7/6 Restful APIs with Passport Auth Example Tutorial

Follow the below following steps and create RESTful authentication APIs in laravel apps with passport:

  • Step 1: Install Fresh Laravel Setup
  • Step2: Configure Database Details
  • Step 3: Install Passport Packages in Laravel
  • Step 4: Run Migration and Install Passport Auth
  • Step 5: Passport Configuration
  • Step 6: Create APIs Route
  • Step 7: Create Controller & Methods
  • Step 8: Now Test Laravel REST API in Postman

Step 1: Install Fresh Laravel Setup

First of all, run the following command on your command prompt to install laravel fresh setup for building laravel 7 restful authentication Apis with passport app:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Configure Database Details

Then, Navigate root directory of your installed laravel restful authentication api with passport tutorial project. And open .env file. Then add the database details as follow:

 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Step 3: Install Passport Package In Laravel

In this step, run the below command and install passport package :

composer require laravel/passport

After successfully install laravel passport, register providers. Open config/app.php . and put the bellow code :

  // config/app.php

'providers' =>[

Before you run migration command, go to the app/providers/AppServiceProvider.php and put the two line of code inside a boot method :

 Use Schema; 
 public function boot() { 

Step 4: Run Migration and Install Passport Auth

In this step, you need to do migration using the bellow command. This command creates tables in the database :

php artisan migrate

Now, you need to install laravel to generate passport encryption keys. This command will create the encryption keys needed to generate secure access tokens. like secret key and secret id.

php artisan passport:install

Step 5: Laravel Passport Configuration

In this step, Navigate to App folder and open User.php file. Then update the following code into User.php:

namespace App;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
use HasApiTokens, Notifiable;
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',

Next Register passport routes in App/Providers/AuthServiceProvider.php, Go to App/Providers/AuthServiceProvider.php and update this line => Register Passport::routes(); inside of boot method:

namespace App\Providers;
use Laravel\Passport\Passport; 
use Illuminate\Support\Facades\Gate; 
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider 

     * The policy mappings for the application. 
     * @var array 
    protected $policies = [ 
        'App\Model' => 'App\Policies\ModelPolicy', 
     * Register any authentication / authorization services. 
     * @return void 
    public function boot() 


Now, Navigate to config/auth.php and open auth.php file. Then Change the API driver to the session to passport. Put this code ‘driver’ => ‘passport’, in API :

         'web' => [ 
             'driver' => 'session', 
             'provider' => 'users', 
         'api' => [ 
             'driver' => 'passport', 
             'provider' => 'users', 

Step 6: Create APIs Route

In this step, you need to create rest API routes for laravel restful authentication apis with passport project.

So, navigate to routes folder and open api.php. Then update the following routes into api.php file:

 Route::post('login', 'Api\AuthController@login');
 Route::post('register', 'Api\AuthController@register');
 Route::group(['middleware' => 'auth:api'], function(){
 Route::post('getUser', 'Api\AuthController@getUser');

Step 7: Create Controller & Methods

In this step, you need to create a controller name AuthController. Use the below command and create a controller :

 php artisan make:controller Api\AuthController

After that, you need to create some methods in AuthController.php. So navigate to app/http/controllers/Api folder and open AuthController.php file. Then update the following methods into your AuthController.php file:

namespace App\Http\Controllers\Api;
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use App\User; 
use Illuminate\Support\Facades\Auth; 
use Validator;
class AuthController extends Controller 
 public $successStatus = 200;
 public function register(Request $request) {    
 $validator = Validator::make($request->all(), 
              'name' => 'required',
              'email' => 'required|email',
              'password' => 'required',  
              'c_password' => 'required|same:password', 
 if ($validator->fails()) {          
       return response()->json(['error'=>$validator->errors()], 401);                        }    
 $input = $request->all();  
 $input['password'] = bcrypt($input['password']);
 $user = User::create($input); 
 $success['token'] =  $user->createToken('AppName')->accessToken;
 return response()->json(['success'=>$success], $this->successStatus); 
public function login(){ 
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){ 
   $user = Auth::user(); 
   $success['token'] =  $user->createToken('AppName')-> accessToken; 
    return response()->json(['success' => $success], $this-> successStatus); 
  } else{ 
   return response()->json(['error'=>'Unauthorised'], 401); 
public function getUser() {
 $user = Auth::user();
 return response()->json(['success' => $user], $this->successStatus); 


 Lets got terminal & run the command :

 php artisan serve

Step 8: Now Test Laravel REST API in Postman

Here, you can see that, how to call laravel 7 restful authentication apis with passport tutorial projects:

Laravel Register Rest API :

laravel register api

laravel register api

Login API :

laravel passport login api

laravel passport login api

Next Step, you will call getUser API, In this API you have to set two headers follows:

 Call login or register apis put $accessToken. 
 ‘headers’ => [
 ‘Accept’ => ‘application/json’,
 ‘Authorization’ => ‘Bearer ‘.$accessToken,

Pass header in login/register rest API. it is necessary to passport authentication in laravel app


Laravel passport tutorial, you have learn how to install a laravel passport authentication package and also configuration passport package in laravel application. As well as using the laravel passport package, how to create rest login authentication API, register API and get user info API.