Laravel jwt Authentication API | Laravel 5.8 Create REST API with jwt Authentication

In this tutorial you will learn about the Laravel jwt Authentication API | Laravel 5.8 Create REST API with jwt Authentication and its application with practical example.

Laravel 5.8 Create REST API with jwt Authentication

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

What is JWT?

JWT stands for JSON Web Tokens. JSON Web Token (JWT) is an authentication token which is used to securely transmit data between third-parties as a JSON object. The JSON Web token usually remains valid for 3600s or one hour. Nowdays API’s are mostly developed with JWT authentication.

Install Laravel 5.8

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

Install JWT Auth Package Using Composer

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

If you are using Laravel version 5.5 or above, run the following command to install jwt package.

If you are using the laravel version 5.4 or less, run the following command to install jwt 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

For versions less than 5.5, you are also required to set service provider and alias in config/app.php file.

If you are using Laravel version 5.5 or above, laravel does it automatically using Package Auto-Discovery.

Publishing Configuration File

Publish the configuration file using the following command for versions 5.5 or above

If you are using previous versions of laravel, run the following command to publish the configuration

Above command will generate a config/jwt.php configuration file.

config/jwt.php

Generate JWT Key

Run the following command for laravel 5.5 or above to generate the secret key used to sign the tokens.

For versions below 5.5

Registering Middleware

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

app/Http/Kernel.php

Update User Model

In this step we will require to implement JWTSubject interface in User model. It is mandatory to implement the Tymon\JWTAuth\Contracts\JWTSubject interface on the User model. This interface requires implementing two methods getJWTIdentifier and getJWTCustomClaims. Let’s update app/User.php model file with the following lines of code.

Recommended:-  Laravel 6 Import Export Excel CSV File to Database

app/User.php

Create Authentication API Routes

In this step, We will setup authentication routes to register a new user, login with their user credentials and get the authenticated user details by using JWT token. So add the following route in routes/api.php

routes/api.php

Create Authentication Controller

Now, create a Authentication Controller name AuthController. Use the below command to create controller.

Once the above command executed, it will create a resource controller file “AuthController.php” in “app/Http/Controllers/” directory. Go to “AuthController.php” and put the following code in it.

Building the Post CRUD API

Next, we will be creating a simple blog application with post table and implement CRUD (Create, Read, Update and Delete) APIs. To create the blog posts APIs we have to define table schema for posts table. Open terminal and use the following artisan command to generate <timestamp>create_posts_table.php migration.

Recommended:-  Simple User Login Example in AngularJS

Once this command is executed you will find a migration file created under “database/migrations”. Lets open migration file and put following code in it –

Now, run following command to migrate database schema.

After, the migration executed successfully the posts table will be created in database.

Create Model

In this step, we will use the following command to create post model –

Once, the above command executed it will create a model file Post.php in app directory. Next, we have to assign fillable fields using fillable property inside Post.php file. Open app/Post.php file and put the following code in it –

app/Post.php

Now, we have to add a relationship in the User model to retrieve the related posts. In app/User.php add the following method.

Create Controller

In this step, we will use the following command to create post controller.

Once the above command executed, it will create a controller file “PostController.php” in “app/Http/Controllers/” directory. Open the PostController.php file and put the following code in it.

app/Http/Controllers/PostController.php

Create Resource Route

After this, we need to add resource route for post controller. Lets open “routes/api.php” file and add following route in it.

our final route file looks as following –

routes/api.php

Start Application Server

Lets start the development server using following artisan command –

Testing the REST API

Our Authentication API is ready to test. I will use Postman to test the API.

Register Api :-
Verb: POST
URL :http://localhost:8000/api/register

register

Login Api :-
Verb: POST
URL :http://localhost:8000/api/login

login

User Api :-
Verb: GET
URL: http://localhost:8000/api/user

userinfo

Add Post Api :-
Verb: POST
URL: http://localhost:8000/api/posts

add_post

List Post Api :-
Verb: GET
URL: http://localhost:8000/api/posts

user_posts

Single Post Api :-
Verb: GET
URL: http://localhost:8000/api/posts/{ID}

user_single_post

Update Post Api :-
Verb: PUT
URL: http://localhost:8000/api/posts/{ID}

user_post_update

Delete Post Api :-
Verb: DELETE
URL: http://localhost:8000/api/posts/{ID}

user_post_delete

In this tutorial we have learn about the Laravel jwt Authentication API | Laravel 5.8 Create REST API with jwt Authentication and its application with practical example. I hope you will like this tutorial.