Laravel 8 Rest API Example With JWT Authentication

Laravel 8 Rest API Example With JWT Authentication

In this tutorial, we are going on how to create the rest of the API Example with JWT authentication in laravel 8(Laravel 8 Rest API Example With JWT Authentication). so here we are using the tymon/jwt-auth package for the rest API.

The Laravel JWT 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 the rest API with authentication in laravel.

Overview

Step 1: Install Laravel
Step 2: Setting Database Configuration
Step 3: Install JWT Package
Step 4: Configure Auth guard
Step 5: Registering Middleware
Step 6: Update the User Model
Step 7: Create Route
Step 8: Create a Controller
Step 9: Run The Application

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

Step 2: 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 3: Install JWT Package
Now, we are going to install the “tymon/jwt-auth” package using the below command.

We will add below providers and aliases in the “config/app.php” file.

After the complete set service provider and aliases, we need to run the below command for publishing the package’s configuration.

Now, we will create a Generate jwt secret key using the below command. It will be automatically configure in .env file using the below command.
Step 4: Configure Auth guard
here, in this step, we have to the update guard. so here we will update the token driver to jwt driver in API guards.
config/auth.php

Step 5: Registering Middleware
Now, we need to add middleware in the routeMiddleware array. so you can see the below code.
app/Http/Kernel.php

Step 6: Update the User Model
Open the User model and update the below code. here we will use the Tymon\JWTAuth\Contracts\JWTSubject package and two methods. like as getJWTIdentifier() and getJWTCustomClaims().
app/Models/User.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 UserController.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/UserController.php

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

Read Also
Laravel 8 CRUD Operation With Ajax Example
Laravel 8 Rest API CRUD Example With Passport Auth
Laravel 8 Pagination Example Tutorial

Now, you can call the rest API using postman. so we shared some screenshots.
Register API
laravel8_jwt_api_register
Login API
laravel8_jwt_api_login
User Get API

laravel8_jwt_api_getuser

Download

Please follow and like us: