API Authentication using Laravel Passport

Updated 6 June 2023

Hello Everyone, In this Article we will learn how to create a fast and secure RESTful Authentication API with Passport Package in Laravel. But before that, we will discuss API and Laravel Passport.

API stands for Application Programming Interface. An API is a way that enables two or more software components to communicate with each other using a set of definitions and protocols. Let’s take an example of a restaurant where the waiter plays the role of middleman between the customer and the kitchen. A waiter takes orders from customers and brings those orders to the kitchen. After preparation, the waiter then brings the customers their food. so the waiter connects the customer and the kitchen. In the same way, an API is a middleman that connects two or more software platforms.

Laravel Passport is an Open Authorization 2.0 server (OAuth2) implementation used for authenticating APIs using Laravel in a matter of minutes. As the tokens are only used for API authentication, Laravel Passport provides an easy way to implement token authentication on the OAuth server.

Let’s see how to set up and configure Laravel Passport for API Authentication in a Laravel application.

Step 1: Install Laravel

Let us begin the tutorial by installing a new laravel application. if you have already installed then you may go ahead.

 

Step 2: Add Database Detail

Go to the .env file and configure your database details. Define the database name, user name, and password of your database.

 

Step 3: Install Passport

In this step, we will install laravel passport package via composer. Run the following command to install the package.

After installation of the package, we have to use the default migration to create a new table in our database. So, run the below command.

Now, we have to run the following artisan command. This command will create the encryption keys needed to generate secure access tokens.

 

Step 4: Passport Configuration

Now, we have to configure our model, service provider, and auth config file.

First, open the app/Models/User.php file and add HasApiTokens class as mentioned below.

Next, open app/Providers/AuthServiceProvider.php file and register the registerPolicies() method inside the boot() function, It will add the required routes.

Now Open config/auth.php configuration file, We have to define an API authentication guard and set the driver option to Passport. This will instruct our application to use Passport TokenGuard when authenticating incoming API requests.

 

Step 5:  Create API Routes

In this step, we will create API routes. Laravel provides an api.php file for web services routes. So, let’s add a route in the api.php file.

 

Step 6: Create Controller files

In this step, we have to create a new controller file as UserController. Please run the following command to create a controller.

After creating the controller we will make authentication Api’s as mentioned below.

 

Step 7: Testing

All the required steps have been done, Now we have to test our API’s. Run the following command to serve the application on the PHP development server.

Now, let’s test our API’s endpoints using an API testing tool like Postman.

 Register Api

 Login Api

User Record Api

When testing user record api we have to add the following code on the header

 

I hope you will like the Tutorial and it will help you to learn API Authentication using Laravel Passport. Please comment below if you have any questions.

You can also hire laravel developers to build your custom solutions on laravel. For exploring the available extensions for Bagisto, you can check out the bagisto extension marketplace.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • snkt
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home