KenoKivabe

Your Essential Queries

Author
Md Mojahedul Islam
06 Aug, 2022

How To Setup Laravel Passport


We'll See How To Setup Laravel Passport Easily:

1) Open Terminal

  1. cd /var/www/project__dir  

2) Install Passport Package

  1. composer require laravel/passport  

3) Migrate Auto Generated Tables for Passport

  1. php artisan migrate  

4) Install Passport Keys

  1. php artisan passport:install

5) Use Passport HasApiTokens trait to User Model

  1. <?php    
  2.     
  3. namespace App\Models;    
  4.     
  5. use Illuminate\Contracts\Auth\MustVerifyEmail;    
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;    
  7. use Illuminate\Foundation\Auth\User as Authenticatable;    
  8. use Illuminate\Notifications\Notifiable;    
  9. use Laravel\Passport\HasApiTokens;    
  10.     
  11. class User extends Authenticatable    
  12. {    
  13.     use HasApiTokens, HasFactory, Notifiable;    
  14.     
  15.     /**   
  16.      * The attributes that should be hidden for serialization.   
  17.      *   
  18.      * @var array   
  19.      */    
  20.     protected $hidden = [ 'password'];    
  21.     
  22. }   

6) Use Passport Routes To AppServiceProvider

  1. <?php  
  2.   
  3. namespace App\Providers;  
  4.   
  5. use Illuminate\Support\ServiceProvider;  
  6. use Illuminate\Support\Facades\Blade;  
  7. use Laravel\Passport\Passport;  
  8.   
  9. class AppServiceProvider extends ServiceProvider  
  10. {  
  11.     /** 
  12.      * Register any application services. 
  13.      * 
  14.      * @return void 
  15.      */  
  16.     public function register()  
  17.     {  
  18.         //  
  19.     }  
  20.   
  21.     /** 
  22.      * Bootstrap any application services. 
  23.      * 
  24.      * @return void 
  25.      */  
  26.     public function boot()  
  27.     {  
  28.         Passport::routes();  
  29.     }  

7) Update Auth Drive in config/auth.php 

  1. 'guards' => [  
  2.                 'web' => [  
  3.                     'driver' => 'session',  
  4.                     'provider' => 'users',  
  5.                 ],  
  6.   
  7.                 'api' => [  
  8.                     'driver' => 'passport',  
  9.                     'provider' => 'users',  
  10.                 ],  
  11.         ], 

Sample Code Using Passport for Login (routes/api.php):

  1. <?php  
  2.   
  3. use Illuminate\Support\Facades\Auth;  
  4. use Illuminate\Support\Facades\Validator;  
  5. use App\Models\User;  
  6.   
  7. Route::post('/user/login'function (Request $request) {  
  8.              $validator = Validator::make($request->all(),[  
  9.           'email' => 'required',  
  10.           'password' => 'required'  
  11.      ]);  
  12.   
  13.      if($validator->fails()) {  
  14.             return response()->json($validator->errors(), 202);  
  15.         }  
  16.   
  17.         if(Auth::attempt(['email'=>$request->email, 'password'=>$request->password])){  
  18.             $user = Auth::user();  
  19.             $res = [];  
  20.             $res['user'] = $user->name;  
  21.             $res['token'] = $user->createToken($user->name)->accessToken; //sending token with response  
  22.             return response()->json($res, 200);  
  23.         } else {  
  24.             return response()->json(['message'=>'Password not matched!']);  
  25.         }  
  26. });  
  27.   
  28. Route::middleware('auth:api')->post('/user/details',function(){  
  29.     return response()->json(User::all());  
  30. });  

Share: