Passport For Your Laravel Flight
Authentication process of every web framework is unique and relate to any general concept for development, same as in laravel echo-system having passport OAuth token based system that will work without storing auth token in session and manage through database.
Steps to be Followed:

- To get started with passport, first install Passport via the Composer package manager:
- Open your terminal/bash/shell
- Change your directory, go to your project root directory and run the below command
composer require laravel/passportThis will install the laravel passport package for you. 2. The Passport service provider registers its own database migration directory.This directory will have migration files to generate the necessary tables. After the installation finished run following command.
php artisan migrateThis will create the tables for your application needs to store clients and access tokens. 3. Once all the needed tables created run the following command.
php artisan passport:install
This command will create the encryption keys needed to generate secure access tokens. In addition, the command will create "personal access" and "password grant" clients which will be used to generate access tokens.
4. After running this command go to your editor and open user model file:Filename:App\User.phpAdd the following line at the top of the file after the namespace declaration.
use Laravel\Passport\HasApiTokens;Add the following line with in the class declaration.
use HasApiTokens;This trait will provide a helper methods to your model which allow you to identify the authenticated user's token and scopes. 5. Next step is, you should call the Passport::routes method within the boot method of your AuthServiceProvider.
Filename : App\Providers\AuthServiceProvider.phpAdd the Following line at the top of the file after the namespace declaration.
use Laravel\Passport\Passport;Add the Following line in to boot method of the file
Passport::routes();This method will register the routes necessary to assign access tokens and revoke access tokens, clients, and personal access tokens. 6. Last but not the least, go to the configuration file i.e auth.php
Filename : config/auth.phpChange the driver name to the "passport" as show below
'api' => [ 'driver' => 'passport', 'provider' => 'users', ],This will set the driver option of the api authentication guard to passport. This will point your application to use Passport's TokenGuard while authenticating incoming API request. 7. Next step which is optional but will help you in error customization, If the token passed in request is wrong then you will always get error “unauthenticated”, So if you want to customize this error msg and error format then go to the exception handler file i.e
Filename : app/Exceptions/Handler.phpGo to render method and add the following code:
if($exception instanceof \Illuminate\Auth\AuthenticationException){ return response()->json(['error' => 'Unauthorised'], 401); } return parent::render($request, $exception);
Comments
Post a Comment