Welcome to our guide on creating Laravel API authentication using Passport! In today’s digital landscape, securing API endpoints and authenticating users is paramount for ensuring data privacy and system integrity. Laravel’s Passport package provides a robust solution for implementing OAuth2 authentication in your Laravel applications, offering a seamless way to authenticate API requests and manage access tokens. Whether you’re building a RESTful API for mobile apps, web services, or other integrations, this tutorial will walk you through the process of setting up Laravel Passport for secure and efficient API authentication. Let’s dive in and explore how to leverage Passport to enhance the security and usability of your Laravel-powered APIs.

Step 1: Install Laravel 8 for REST API with Passport Authentication

In this step, we will create laravel 8 application using the below command.

composer create-project –prefer-dist laravel/laravel REST_API

Step 2: Install Passport

Now, We are required to install the passport via the composer package manager. So, in your terminal run the below command.

composer require Laravel/passport

After installation of the package, We are required to get default migration to create new passport tables in our database. So, run the below command.

php artisan migrate

Now, We will install the passport using the passport:install command. which will create token keys for security.

php artisan passport:install

Step 3: Passport Configuration

Now, We have to configure the model, service provider, and auth config file.

  1. HasApiTokens class of Passport has been added in the User model,

Add the below code in app/User.php

create_A_record

  1. In AuthServiceProvider we have added “Passport::routes()”

Add the below code in the app/Providers/AuthServiceProvider.php

create_A_record

  1. We have added API auth configuration in the auth.php file.

In config/auth.php add the below code.

create_A_record

Step 4: Add Table and Model

In this step, We will create migration of the Product table using the PHP artisan command. So, run the below command in your terminal.

php artisan make:migration create_products_table

After running the above command, you will find migration in this path database/migrations. So, add the below code in your migration file to create the products table.

create_A_record

Now, run the below code in the terminal to create a migration.

php artisan migrate

And add the below code in the app/Product.php file

create_A_record

Step 5: Create API Routes

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

create_A_record

Step 6: Create Controller

Now, I have created BaseController, ProductController, and RegisterController in API folder.

app/Http/Controllers/API/BaseController.php

create_A_record

app/Http/Controllers/API/ProductController.php

create_A_record

create_A_record

create_A_record

app/Http/Controllers/API/RegisterController.php

create_A_record

create_A_record

Now, I have used POSTMAN API for testing purposes, Postman is a collaboration platform for API development. Postman’s features simplify each step of building an API and streamline collaboration so you can create better APIs—faster.

Here I have added some screenshots with descriptions of the postman for your better understanding. First of all, we need to register in postman to check our example.

1 – Register API: Verb:GET, URL:http://localhost:8000/api/register

create_A_record

2 – After successfully registering in postman we need to add an access token in headers.

create_A_record

3 – Login API: Verb:GET, URL:http://localhost:8000/api/login

create_A_record