Laravel 5.8 Passport oAuth 2.0 Usage and Implementation

STEP 1: Implementation
You must follow the steps as explained in the documentation.
It provides the following grant types out of the box. Laravel passport with password grant tokens
1- Implicit Grant Tokens: Used when your app/website needs to use data from client’s profile or account (mostly). Like signup using facebook. Or posting something on facebook from a 3rd party.
2- Client Credentials Grant Tokens: The client credentials grant is suitable for machine-to-machine authentication. can be generated only from the command line.
3- Personal Access Tokens: Sometimes, your users may want to issue access tokens to themselves without going through the typical authorization code redirect flow as mentioned in (1).
4- Modified Client_Credentials Token: I wanted the users to sign up and communicate between machine to machine, without the involvement of any mediator and put a possible subscription as well, all while keeping the original settings intact and avoid the problem and solving the error.

STEP 2: Needed Modifications
Now, move all the api.php routes associated with client credential out of:
/routes/api.php
and move them into a new route file named:
/routes/client_credentials.php
doing this makes sure nothing of the core functionality is affected.
Next, we need to use this new route file (client_credentials.php). To do this, update:
/app/Providers/RouteServiceProvider.php
by adding a new method and calling it in the existing “map” method as shown below in file RouteServiceProvider.php
mapApiRoutes();
$this->mapWebRoutes();

    // ref new method for adding client credentials
    $this->mapClientCredentialRoutes();
}

// new client credentials method
protected function mapClientCredentialRoutes()
{
Route::prefix(‘api’) // I still want /api/ urls
->middleware(‘client_credentials’) // new middleware I’ll set up in a bit
->namespace($this->namespace)
->group(base_path(‘routes/client_credentials.php’)); // referencing my new routes file
}
}
Now modify the kernel file to allow above file to be part of the routing system.
/app/Http/Kernel.php
Here create a new middleware group for client_credentials for our new middleware.
[
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
‘api’ => [
‘throttle:60,1’,
‘bindings’,
],

    // NEW LINE
    'client_credentials' => [
        \Laravel\Passport\Http\Middleware\CheckClientCredentials::class,
        'throttle:60,1',
        'bindings',
    ]
];

}
STEP 3: USAGE
Now for the usage part.
Create a new client credential from the default vue component

Secret keys to be used only in PHP server-side scripting
Implement the functions as follows.
<?php

use App\User;
use Illuminate\Http\Request;

Route::middleware(‘client_credentials’)->get(‘/user’, function (Request $request) {
return response()->json([“data”=>true]);
});
Route::middleware(‘client_credentials’)->get(‘/user/get’, function(Request $request){
$user_id = $request->get(“uid”);
$user = User::where(‘id’,’=’,$user_id)->get()->first();
return response()->json($user);
});