Categories
Laravel

Tutorial Install Oauth2 Server

Find below the steps to install an Oauth2 server with laravel Passport

Install the package

composer require laravel/passport
php artisan migrate
php artisan passport:install

Retrieve the keys from laravel passport

Personal access client created successfully.
 Client ID: 7
 Client secret: JfTAiRtvsdX2pZI1cBUShCrd2BU5pYYrX0lzHwRhBq
 Password grant client created successfully.
 Client ID: 8
 Client secret: Y3nUHg1xcaOPCsdsdsqbIjA3Ghj5CJnTf0pD1p3t2U5wN

Modify App\User.php file

<?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; 
}

Register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:

<?php 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()     
   {
      $this->registerPolicies();   
      Passport::routes();    
   } 
}

Configure config/auth.php

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

SET TO GO, now get access token to proceed

Use the generated key in initial step called Password grant client

Categories
Access Token API Laravel Oauth PHP Valet Web API

Laravel Passsport – Oauth2

Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes. Passport is built on top of the League OAuth2 server that is maintained by Andy Millington and Simon Hamp.