laravel passport

Laravel-Passport

Laravel Passport:

Laravel passport basically provides you to an OAuth2 server implementation. For more about OAuth2.0 server visit this link https://oauth2.thephpleague.com/.

 

Why Laravel Passport not OAuth2 server?

OAuth2 server provides you to various standards for your API authentication but it complex and would be a difficult to implement. While in passport is native OAuth2 server of laravel apps and it easy to learn and easier to implement in your app.

 

What is Laravel-Passport?

In laravel framework it already comes with the tradition Login form. But what about an API authentication? So for authenticate a User in API we need a tokens. In general API need a token for accessing the user or authenticate a users. So here Laravel comes with a Laravel-passport, which provides the full OAuth2 server implementation in a less time.

 

What does OAuth2 server?

OAuth2 serve protect your API with access token or allow clients to request new access token and refresh them.

Some grants supported by OAuth2 server are:

·         Authorization code grant

·         Implicit grant

·         Client credential grant

·         Resource Owner password credential grant

·         Refresh grant

 

Note: Installing passport in your application I recommend you to visit laravel documentation and follow the instruction. It is easier to setup in your application. Link is https://laravel.com/docs/7.x/passport

 

Make an API using Laravel-Passport:

Now here I have put simple example using Laravel passport how you can make a Login API and for the API testing I am using the postman. Just follow the steps:

·         First setup your project with auth scaffolding and install the passport package in your application, visit this link https://laravel.com/docs/7.x/passport.

·         After installing and setup the scaffold simple register the user in your project.

·         Next, create the API routes. Laravel provide the api.php file for creating the api routes just like web.php.

·         Then create a controller for this route. Run the command:

php artisan make:controller Api\apiController

This command will create an apiController under the API directory in your controller directory. Then open this file and write code for login and return and response data in a format of JSON.

·         Now our login API is created. So let’s run the php artisan serve command in your terminal and run the url in the postman. So open postman and paste the url: http://127.0.0.1:8000/api/login

After open the postman then create two parameter fields and put the credential in postman like: email and password.

Next, after filling the details then click on send button. You will get the token for your client.

This is the end of this article, Using this token you can access your API what will you write in your code. This type of authentication is used for authenticate the client.


Comments

Popular posts from this blog

Create Virtual Host in Localhost

User Active/Inactive using Jquery