Laravel 6 REST API With Passport Authentication Tutorial

Laravel 6 REST API With Passport Authentication Tutorial

In this tutorial, we are going on how to create rest API using passport authentication in laravel 6. so here we are using the laravel/passport package for rest API.

The Laravel Passport package is provided by laravel framework. so we can easily create and manage the API in laravel. let’s follow the below steps to how to create rest API with authentication in laravel.

Overview

Step 1: Install Laravel
Step 2: Install Package
Step 3: Add Service Provider
Step 4: Setting Database Configuration
Step 5: Run Migration Command and Passport Install
Step 6: Passport Configuration
Step 7: Create Route
Step 8: Create a Controller
Step 9: Run The Application

Step 1: Install Laravel
We are going to install laravel 6, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.

Step 2: Install Package
Now, we are going to install the “laravel/passport” package using the below command.

Step 3: Add Service Provider
We will add below providers and aliases in the “config/app.php” file.

Step 4: Setting Database Configuration

After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.

Step 5: Run Migration Command and Passport Install

Now, create the migrate table using the below command and we will create a token key for security using the passport install command.

Step 6: Passport Configuration
here, in this step, we have to the configuration in User.php, AuthServiceProvider.php and auth.php. so you can follow the below code.
app/User.php

app/Providers/AuthServiceProvider.php

config/auth.php

Step 7: Create Route

Add the following route code in the “routes/api.php” file.

Step 8: Create a Controller
Now, we need to create an API directory and controller file, so first we will create an API directory and AuthController.php file. after then created a file then we will create the rest API method. so you can follow the below code.
app/Http/Controllers/API/AuthController.php

Step 9: Run The Application
We can start the server and run this application using the below command.

Read Also
Laravel 6 CRUD Operation With Ajax Example

Laravel 6 Pagination Example Tutorial

Now, you can call the rest API using postman. so we shared some screenshot.
Register Api
register api
Login Api
login api
User Get Api
user get api

Download

Please follow and like us: