Laravel 7/6 REST API With Passport Auth Tutorial

In this tutorial you will learn about the Laravel 7/6 REST API With Passport Auth Tutorial and its application with practical example.

In this Laravel 7/6 REST API With Passport Auth Tutorial, i will show you how to create rest api in laravel 7/6 application with passport authentication. In this tutorial we will be using passport for api authentication. we will create register and login api with simple retrieve user details.

Laravel comes with default login authentication, but when we want to create APIs we have to use tokens instead of sessions for authenticating users, as APIs does not support session variables. After login via API, user must be assigned a token and sent back to the user which is further used for authenticating API requests. Laravel provides Passport authentication which makes it easy creating REST APIs in laravel.

Laravel 7/6 REST API With Passport Auth Tutorial

In this step by step tutorial I will demonstrate you how to create REST API With Passport authentication in laravel.

  • Step 1: Install Fresh Laravel Setup
  • Step2: Configure Database Details
  • Step 3: Install Passport Packages in Laravel
  • Step 4: Run Migration and Install Passport Auth
  • Step 5: Passport Configuration
  • Step 6: Create APIs Route
  • Step 7: Create Controller & Methods
  • Step 8: Now Test Laravel REST API in Postman

Step 1: Install Fresh Laravel Setup

First of all we need to create a fresh laravel project, download and install Laravel using the below command

Step 2: Configure Database Details

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Step 3: Install Passport Package In Laravel

In this step, we need to install Laravel Passport package via the composer dependency manager. Use the following command to install passport package.

After successfully install laravel passport, register providers. Open config/app.php . and put the bellow code :

Before you run migration command, go to the app/providers/AppServiceProvider.php and put the two line of code inside a boot method :

Step 4: Run Migration and Install Passport Auth

In this step we will migrate default database schema, run following command to migrate database schema.

Now, you need to install laravel to generate passport encryption keys. This command will create the encryption keys needed to generate secure access tokens. like secret key and secret id.

Step 5: Laravel Passport Configuration

Now, go to App folder and open User.php file. Then put the following code into User.php:

now, we will Register passport routes in App/Providers/AuthServiceProvider.php, Go to App/Providers/AuthServiceProvider.php and update this line => Register Passport::routes(); inside of boot method

config/auth.php

Now, go to config/auth.php and open auth.php file. Now, Change API driver to the session to passport. Put this code ‘driver’ => ‘passport’, in API

Step 6: Create APIs Route

Now, go to routes folder and open api.php. Then update the following routes into api.php file:

Step 7: Create Controller & Methods

Now, lets create a controller named AuthController using command given below –

Now, we need to create some methods in AuthController.php. So navigate to app/http/controllers/Api folder and open AuthController.php file. Then update the following methods into your AuthController.php file:

Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Test Laravel 8 REST CRUD API with Passport Auth in Postman

Now, we will call above create crud and auth apis in postman app:

1 – Laravel Register Rest API :

Laravel 7/6 REST API With Passport Auth Tutorial

2 – Login API :

Laravel 7/6 REST API With Passport Auth Tutorial

Next Step, you will call getUser, create product, list product, edit product, and delete product APIs, In this apis need to pass the access token as headers:

In this tutorial we have learn about the Laravel 7/6 REST API With Passport Auth Tutorial and its application with practical example. I hope you will like this tutorial.