Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

JSON Web Token (JWT) Authentication via API for Laravel & Lumen.

JWT (JSON WEB TOKEN)

JSON Web Token (JWT) is an open standard that allows two parties to securely send data and information as JSON objects. This information can be verified and trusted because it is digitally signed. It is a Token format standardized by the IETF organization.

JWT authentication has added the wider adoption of stateless API services. It makes it convenient to authorise and verify clients accessing API resources. It is a critical part of the authentication system in javascript powered applications.

To Install JWT in your Laravel Project, Just Follow the Below Steps :-

Step 1. Install via composer

Run the following command to pull in the latest version:

composer require tymon/jwt-authCode language: JavaScript (javascript)

Step 2. Add service provider ( Laravel 5.4 or below )

Add the service provider to the providers array in the config/app.php config file as follows:

Step 3. Publish the config

Run the following command to publish the package config file:

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"Code language: JavaScript (javascript)

You should now have a  config/jwt.php  file that allows you to configure the basics of this package.

Step 4. Generate secret key

I have included a helper command to generate a key for you:

php artisan jwt:secretCode language: CSS (css)

This will update your .env file with something like JWT_SECRET=foobar

It is the key that will be used to sign your tokens. How that happens exactly will depend on the algorithm that you choose to use.

Step 5. Bootstrap file changes.

Add the following snippet to the bootstrap/app.php  file under the providers section as follows:

// Add this line
$app->register(App\Providers\AuthServiceProvider::class);Code language: PHP (php)

Step 6. Re-Generate secret key

I have included a helper command to generate a key for you:

php artisan jwt:secretCode language: CSS (css)

This will update your .env file with something like JWT_SECRET=foobar

It is the key that will be used to sign your tokens. How that happens exactly will depend on the algorithm that you choose to use.

Step 7. Update your User model

Firstly you need to implement the Tymon\JWTAuth\Contracts\JWTSubject contract on your User model, which requires that you implement the 2 methods getJWTIdentifier() and getJWTCustomClaims().

The example below should give you an idea of how this could look. Obviously you should make any changes, as necessary, to suit your own needs.

Step 8. Configure Auth guard

Note: This will only work if you are using Laravel 5.2 and above.

Inside the config/auth.php file you will need to make a few changes to configure Laravel to use the jwt guard to power your application authentication.

Make the following changes to the file:

Here we are telling the api guard to use the jwt driver, and we are setting the api guard as the default.

We can now use Laravel’s built in Auth system, with jwt-auth doing the work behind the scenes!

Step 9. Add some basic authentication routes

First let’s add some routes in routes/api.php as follows:

Step 10. Create the AuthController

Then create the AuthController, either manually or by running the artisan command:

php artisan make:controller AuthControllerCode language: CSS (css)

Then add the following:

All Done.

Now you can test API Login/Register with Postman.

Keep Coding 😀😀😀

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

What is Laravel and use cases of Laravel?

What is Laravel? Laravel is an open-source, free PHP web framework designed for the development of web applications following the model-view-controller (MVC) architectural pattern. It was generated…

Read More

Complete guide of Laravel certification courses, tutorials & training

What is Laravel Laravel is a reliable and simple to use open-source PHP framework. It adheres to the model-view-controller pattern of design. Laravel makes use of pre-existing…

Read More

Top 100 laravel interview questions and answers

1) What is Laravel? Laravel is an open-source widely used PHP framework. The platform was intended for the development of web application by using MVC architectural pattern….

Read More

[SOLVED] Laravel : Supervisor FATAL/BACKOFF Exited too quickly (process log may have details)

Problem I’m trying to use Laravel queues with a supervisor but the service is not working properly. When I try to check status:$ sudo supervisorctl status$ laravel-worker:laravel-worker_00:…

Read More

How to Login with Token in Laravel PHP Framework?

How to Login with Token in Laravel PHP Framework? Step 1. Create a new Project, so write down the following command on Git Bash: Step 2. Move…

Read More

Directory Structure Of Laravel Application

devopsschool – This is Project Folder app – The app directory contains the core code of your application. bootstrap – The bootstrap directory contains the app.php file…

Read More