Open command line and "composer require paragonie/random_compat=~2.0" run this command
After setup random compat
Install laravel passport composer require laravel/passport
After install
run this command "php artisan migrate"
Then define passport in config/app.php file
Like as
add this code in provider section
Laravel\Passport\PassportServiceProvider::class,
Then run command
php artisan passport:install
Now we get 2 client id & encryption key
After running this command, add the "Laravel\Passport\HasApiTokens" trait to your App\User model.
add this ato top in controller
"use Laravel\Passport\HasApiTokens;"
add this in class in controller
"use HasApiTokens, Notifiable;"
Next, you should call the Passport::routes method within the boot method of your AuthServiceProvider.
Then open the file "AuthServiceProvider.php" in Providers folder
add "use Laravel\Passport\Passport;" it top
and
public function boot(){
$this->registerPolicies();
Passport::routes();
}
Finally, in your config/auth.php configuration file, you should set the driver option of the api authentication guard to passport.
api driver changed to passport
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
After implements passport add a middleware in the routes which provide apis (routes/api.php)
Route::middleware(['auth:api'])->group(function () {
Route::apiResource('services','Api\ServiceController');
});
Token Lifetimes
public function boot(){
$this->registerPolicies();
Passport::routes();
Passport::tokensExpireIn(now()->addDays(15));
Passport::refreshTokensExpireIn(now()->addDays(30));
}
Then run the passport client
php artisan passport:client (give user id)
get Client secret
get the access token (in postman)
first set the client at body/form-data in postman and set data
grant_type = client_redentials
client_id = 1
client_secret =Client secret
localhost/project/oauth/token (post)