In this tutorial you will learn about the Laravel 8 User Login Signup API with JWT Authentication and its application with practical example.
In this Laravel 8 User Login Signup API with JWT Authentication Tutorial I’ll show you how to build the user login and signup rest APIs with jwt (JSON web token) authentication in laravel 8. In this tutorial you will learn to create login and signup api using jwt 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 this article, we will learn to create fully functional restful API with JWT Authentication in Laravel 8.
Laravel 8 User Login Signup API with JWT Authentication
In this step by step tutorial I will demonstrate you with example to create registration and login api with jwt authentication in laravel. Please follow the instruction given below:
- Install Laravel Application
- Database Connection
- Add User into MySQL Database
- Install & Configure JWT Authentication Package
- Set Up User Model
- Configure Auth Guard
- Build Authentication Controller
- Add Routes for Authentication
- Test REST API with Postman
- Conclusion
Install Laravel Application
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel laravel-jwt-auth --prefer-dist |
Database Connection
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.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= |
Add User into MySQL Database
Now, run following command to migrate database schema.
1 |
php artisan migrate |
Install & Configure JWT Authentication Package
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.
1 |
composer require tymon/jwt-auth |
After Installing tymon/jwt-auth package, we need to add service provider and alias in config/app.php file as following. Include the JWTAuth and JWTFactory facades inside the aliases array as following:
config/app.php
1 2 3 4 5 6 7 8 9 10 11 |
'providers' => [ .... .... Tymon\JWTAuth\Providers\LaravelServiceProvider::class, ], 'aliases' => [ .... 'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class, 'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class, .... ], |
After that, run the below given command to publish the configuration file in Laravel for jwt auth:
1 |
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" |
Now, you need to generate jwt encryption keys. Use the following command to generate encryption keys needed to generate secure access tokens: