How to make API using Laravel Passport?

Laravel is an open-source PHP framework, created by Taylor Otwell and is used for developing web applications.

API’s are now an important part of web development. An API basically specifies how software modules should interact. It helps developers to link applications to database and for user management, social app integration, push notifications, mobile app development and any other 3 rd party integration

Laravel passport basically provides an OAuth2 server implementation. OAuth2 provides various standards for API authentication but is complex and is difficult to implement. Laravel passport is native OAuth2 server of laravel apps and is easy to learn and implement.

What is Laravel Passport?
In laravel framework there is a built-in Login form. To develop API authentication and for authenticating a User in API, we need a tokens. In general API needs a token for accessing the user or authenticate a users. So Laravel framework comes with a Laravel passport package,
which provides the full OAuth2 server implementation.

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

Installing passport in your application pl. visit laravel documentation and follow the instruction. It is easier to setup in your application – https://laravel.com/docs/7.x/passport

Make an API using Laravel-Passport:
APIs typically use tokens to authenticate users and do not maintain session state between requests. A simple example using Laravel passport shows how you can make a Login API and for the API testing using the postman.

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, 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.

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.

As software development continues to become more and more integrated and complex companies will see see the number of API’s usage increase. So it is very important to integrate API’s in all software products.

In case of any clarifications on developing the API, pl. contact us or write in comments section below.

Resources:
Laravel Passport – https://laravel.com/docs/7.x/passport
Postman – https://www.postman.com/
OAuth2 – https://oauth.net/2/