Laravel Passport is an OAuth2 server and API authentication package that is simple and enjoyable to use. If your application absolutely needs to support OAuth2, then you should use Laravel Passport. Laravel Passport provides a full 0Auth2 server implementation. With it, you can easily generate a personal access token to uniquely identify a currently authenticated user. 1. InstallationFor installing this package Run this command from the Terminal: composer require laravel/passport Once the installation is complete, passport migrations will create the tables your application needs to store clients and access tokens.Run the following command to migrate your database: php artisan migrate 2. Generate keysThis command will create the encryption keys needed to generate secure access tokens. php artisan passport:install After the installation process, add the Laravel\Passport\HasApiTokens trait to your App\User model as shown here: Now, to register the routes necessary to issue and revoke access tokens, call the Passport::routes method within the boot method of your AuthServiceProvider. To do this, open the app/Providers/AuthServiceProvider file and update its content as shown below: Finally, open config\auth.php and set the driver of API to passport in the guards array shown below. 3. Create API RoutesLaravel provides routes/api.php file for writing web services routes. So, add a new route on that file. 4. Create API ControllerCreate function in your controller file
5. Test API in PostmanAdd Request data in postman for Login The API will return a token, which we will use in further requests as our means to authentication. For logout using this token In Postman, the “Authorization” tab has a drop-down where the type can be set to “Bearer Token,” after which the token can go into the token field. And you will successfully log out. Official DocumentationFor more information and details you can read the official document from Laravel website. The post Create API Rest with Laravel Passport Authentication appeared first on Lets Nurture - An IT Company Nurturing Ideas into Reality. Create API Rest with Laravel Passport Authentication published first on https://guestspy.tumblr.com/ via Tumblr Create API Rest with Laravel Passport Authentication
0 Comments
Leave a Reply. |
About UsAndroider is small company with a relentless focus on our customers’ success, with customer feedback directly driving innovations in our product. And while we’re proud of our growth, we’re doubly proud of the workplace that we’ve become and the work environment that we foster – just read what our employees have to say about working here. |