In this blog, we discuss how to use the JWT for the authentication of Laravel applications. Laravel is a popular PHP framework. It is mainly used for developing web applications. The core feature of Laravel is that it has built-in authentication functionality. This functionality helps to create simple and secure applications. In this article, we will look at how to create a Laravel application that uses JWT authentication.
JWT (JSON Web Token) is a popular authentication technique seen in current online applications. JWTs are a safe means for parties to exchange data and are often used for authentication and authorization purposes.
The topics covered in this documentation are:
- Setting up a Laravel project
- Installing the JWT authentication package
- Creating and migrating database tables
- Creating authentication routes and controllers
- Generating JWT token and authenticating users
- Protecting routes with JWT authentication
- Testing the authentication
Sample:
As an example, we will build a simple API that allows users to create and view posts. The API will be secured with JWT authentication to ensure that only authenticated users can access the protected routes.
Setting up a Laravel project:
To set up a Laravel project, we need to install the Laravel framework and its dependencies. We can use Composer to install Laravel by running the following command:
composer create-project --prefer-dist laravel/laravel blog
Installing the JWT authentication package:
To use JWT authentication in our Laravel application, we need to install the tymon/jwt-auth package. To install the package using Composer run the following command:
composer require tymon/jwt-auth
Creating and migrating database tables:
We will create two database tables: users and posts. The users table will store user information and the posts table will store post information. We can create these tables using Laravel’s built-in migration functionality. Run the following command to create migrations for the users and posts tables:
php artisan make:migration create_users_table --create=users
php artisan make:migration create_posts_table --create=posts
Creating authentication routes and controllers:
We will create authentication routes and controllers to handle user authentication. To create authentication controllers run the following command:
php artisan make:controller AuthController
Then create routes for login and register in the “routes/api.php” file:
Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');
Generating JWT token and authenticating users:
We will generate JWT token for authenticated users and use them to protect our routes. Run the following command to generate JWT secret key:
php artisan jwt:secret
We will then create a middleware to authenticate users and protect our routes:
Finally, we will add the Authenticate middleware to our routes:
Route::middleware('auth.jwt')->group(function () {
Route::get('posts', 'PostController@index');
Route::post('posts', 'PostController@store');
});
Protecting routes with JWT authentication:
We will use the Authenticate middleware to protect our routes. The middleware will check if the user has a valid JWT token before allowing access to the protected routes.
Testing the authentication:
We can use Postman to test our API. At first register a new user by sending a POST request to the “register” route. Then log in by sending a POST request to the “login” route. After successful authentication, we can access the protected routes by sending requests with the JWT token in the Authorization header.
Till now, we have covered how to use a JWT authentication of Laravel application. So if you want to build a secure web application, reach Sreyas, we are the leading web and mobile application, developers. We provide the best UI/UX designs for your application that make the app more attractive and user-friendly. We provide worldwide support and services to all our satisfied customers.