Install and use Laravel Passport tutorial

Tram Ho

Preamble

Laravel Passport is built on top of the League OAuth2 server developed by Andy Millington and Simon Ham.

Setting

  • Install Passport via the Composer package manager:

Laravel Passport provides several migration classes to create the necessary tables to store authorization codes, access tokens, refresh tokens, personal access tokens, and client information (a list of migration files can be found here). To install the necessary tables for Laravel Passport, we use the following command:

Next, you should execute the passport:install Artisan command. This command will generate the encryption keys needed to generate a secure access token

After running passport:install , add LaravelPassportHasApiTokens to AppModelsUser

Finally, in your application config/auth.php file you should define an authentication guard api and set the optional driver to passport

Token Lifecycle Configuration By default, Passport issues a lifetime access token that expires after one year. If you want to configure longer/shorter token lifetime, you can use tokensExpireIn, refreshTokensExpireIn and personalAccessTokensExpireIn methods in AppProvidersAuthServiceProvider

Create Api Routes

Create Controller First we will create the file AuthController.php in the Controller folder

Reference source

https://laravel.com/docs/8.x/passport

Share the news now

Source : Viblo