Beginning JWT in Laravel

Intsall the followoing package in your Laravel project

First you can install the third-party JWT package by running the following command in your terminal

Open config/app.php and add the following provider to the providers array:

Use the following command to publish JWT provider

Now you can use following command to generate token

Now you can create the User model. Simply edit the code loacted at App\User.php

Create the Middleware

Now you can create the Middleware to filter the request comming to our API
If you are not familier with Laravel Middleware you can follow this tutorial

Create Controllers

You can create two Controlls in this exaple. One is to acesss the User model and other is to acess the data in your project

UserController class will have three methods
register()
This method will create new user in system by passing the email and password parameter

login()
This method can be used to authenticate the user by passing email and password. Once authentication is done you can get the token

getAuthenticatedUser()
You can use this method to get the authenticated user details by passing the token

Here is the full code of the UserController

Now we will add two methods to DataController class
I am going to add two methods to this class called open() and closed(). open() method can be accessed without authenticating the user and close() method is only for authenticated users

Now you can test on POSTMAN