Laravel 8 JWT Rest API Authentication Example Tutorial

In this tutorial you will learn about the Laravel 8 JWT Rest API Authentication Example Tutorial and its application with practical example.

In this Laravel 8 JWT Rest API Authentication Example Tutorial I’ll show you how to build the rest APIs with jwt (JSON web token) authentication in laravel 8. In this example I’ll also show you how to install jwt auth and configure jwt auth in laravel 8.

In one of my previous articles, we have learn How to Create REST API With Passport Authentication In Laravel 8 using Laravel passport for REST API authentication. In this article, we will learn to create fully functional restful API with JWT Authentication in Laravel 8. In this tutorial, we will be creating fully functional REST API along with JWT Authentication.

Laravel 8 JWT Rest API Authentication Example Tutorial

In this laravel step by step tutorial you will learn how to create REST API with Laravel 8 using JWT Token (JSON Web Token). Please follow the step given bellow:

  • Download Laravel 8 App
  • Database Configuration
  • Install JWT Auth
  • Registering Middleware
  • Run Migration
  • Create APIs Route
  • Create JWT Auth Controller
  • Now Test Laravel REST API in Postman

Install Laravel 8

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

Configure Database

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.

.env

Install JWT Auth

In this step, we will install tymon jwt auth package via the composer dependency manager. Use the following command to install laravel jwt authentication package.

After Installing tymon/jwt-auth package, we need to add service provider and alias in config/app.php file as following.

config/app.php

Now, you need to generate jwt encryption keys. Use the following command to generate encryption keys needed to generate secure access tokens:

Now, open JWTGenerateCommand.php file and put the following code,

vendor/tymon/src/Commands/JWTGenerateCommand.php

Registering Middleware

Register auth.jwt middleware in app/Http/Kernel.php

app/Http/Kernel.php

Run Migration

Now, you need to run migration using the following command to create tables in the database :

Create APIs Route

Now we will create rest API auth routes. Go to the routes directory and open api.php. Then put the following routes into api.php file:

routes/api.php

Create JWT Auth Controller

In this step, we will create a controllers name JWTAuthController. Use the following command to create a controller :

After that, Create some authentication methods in JWTAuthController.php. So navigate to app/http/controllers/API directory and open JWTAuthController.php file. And, update the following methods into your JWTAuthController.php file:

Then open command prompt and run the following command to start developement server:

Test Laravel 8 REST API with JWT Auth in Postman

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

1 – Laravel Register Rest API :

Laravel 8 REST API with JWT Auth 1

2 – Login API :

Laravel 8 REST API with JWT Auth 2

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 8 JWT Rest API Authentication Example Tutorial and its application with practical example. I hope you will like this tutorial.