DEV Community

Cover image for Secure Your Laravel App with JWT Authentication
Abdelhakim Baalla
Abdelhakim Baalla

Posted on • Edited on

1

Secure Your Laravel App with JWT Authentication

Learn how to implement JWT authentication in Laravel step by step. A beginner-friendly guide with code examples and best practices.

Introduction

Authentication is a crucial part of web applications, ensuring that only authorized users can access protected resources. One of the most popular ways to handle authentication in modern applications is by using JSON Web Tokens (JWT).

In this guide, we’ll walk you through how to implement JWT authentication in Laravel. Whether you're a complete beginner or just looking for an easy-to-follow tutorial, this guide is for you!


What is JWT and Why Use It?

JSON Web Token (JWT) is a secure and compact way to transmit authentication data between a client and a server. Here’s why it’s widely used:

Stateless Authentication – No need to store sessions on the server.

Secure – Uses encryption to protect sensitive user information.

Cross-platform – Works with mobile apps, SPAs, and APIs.

When a user logs in, the server generates a JWT token, which is then stored in the client (e.g., local storage or HTTP headers). On subsequent requests, the token is sent to verify the user's identity.


Step 1: Set Up a New Laravel Project

First, make sure you have Laravel installed. If not, install it using Composer:

composer create-project --prefer-dist laravel/laravel jwt-auth-app
Enter fullscreen mode Exit fullscreen mode

Navigate to your project directory:

cd jwt-auth-app
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Laravel JWT Package

We’ll use tymon/jwt-auth, a popular package for handling JWT authentication in Laravel.

Run the following command:

composer require tymon/jwt-auth
Enter fullscreen mode Exit fullscreen mode

Now, publish the package configuration:

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
Enter fullscreen mode Exit fullscreen mode

Generate the JWT secret key:

php artisan jwt:secret
Enter fullscreen mode Exit fullscreen mode

This key is stored in your .env file under JWT_SECRET.


Step 3: Configure Authentication Guards

Open the config/auth.php file and update the guards array:

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],
Enter fullscreen mode Exit fullscreen mode

This tells Laravel to use JWT for API authentication.


Step 4: Set Up User Model for JWT

Modify the User.php model to implement JWTSubject:

use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Create Authentication Controller

Now, let’s create an authentication controller to handle login, register, and logout.

Run:

php artisan make:controller AuthController
Enter fullscreen mode Exit fullscreen mode

Inside AuthController.php, add the following methods:

User Registration

public function register(Request $request)
{
    $validatedData = $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);

    $user = User::create([
        'name' => $validatedData['name'],
        'email' => $validatedData['email'],
        'password' => bcrypt($validatedData['password']),
    ]);

    $token = auth()->login($user);

    return response()->json(['token' => $token]);
}
Enter fullscreen mode Exit fullscreen mode

User Login

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (!$token = auth()->attempt($credentials)) {
        return response()->json(['error' => 'Unauthorized'], 401);
    }

    return response()->json(['token' => $token]);
}
Enter fullscreen mode Exit fullscreen mode

User Logout

public function logout()
{
    auth()->logout();
    return response()->json(['message' => 'Successfully logged out']);
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Set Up Routes

Open routes/api.php and add the following routes:

Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);
Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth:api');
Enter fullscreen mode Exit fullscreen mode

Step 7: Protect Routes with JWT Middleware

To secure API routes, apply the auth:api middleware:

Route::middleware('auth:api')->group(function () {
    Route::get('/profile', function () {
        return response()->json(auth()->user());
    });
});
Enter fullscreen mode Exit fullscreen mode

Now, only authenticated users can access /profile.


Step 8: Testing JWT Authentication

Test Registration

Send a POST request to:

POST http://127.0.0.1:8000/api/register
Enter fullscreen mode Exit fullscreen mode

With this JSON payload:

{
    "name": "John Doe",
    "email": "johndoe@example.com",
    "password": "password123",
    "password_confirmation": "password123"
}
Enter fullscreen mode Exit fullscreen mode

Test Login

POST http://127.0.0.1:8000/api/login
Enter fullscreen mode Exit fullscreen mode

Payload:

{
    "email": "johndoe@example.com",
    "password": "password123"
}
Enter fullscreen mode Exit fullscreen mode

It returns a JWT token:

{
    "token": "your-jwt-token-here"
}
Enter fullscreen mode Exit fullscreen mode

Test Accessing a Protected Route

Use the token to access /profile:

GET http://127.0.0.1:8000/api/profile
Enter fullscreen mode Exit fullscreen mode

Set the Authorization header:

Authorization: Bearer your-jwt-token-here
Enter fullscreen mode Exit fullscreen mode

If successful, it returns the authenticated user’s details.


Conclusion

Congratulations! 🎉 You've successfully implemented JWT authentication in Laravel. Now, your API is secure, and users can authenticate using tokens.

Key Takeaways:
✅ Installed and configured JWT in Laravel

✅ Created authentication routes

✅ Protected API endpoints with JWT middleware


Follow Me for More!

If you found this guide helpful, follow me for more tutorials on web development and programming:

Let’s connect and grow together! 🚀

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Please consider leaving a ❤️ or a friendly comment if you found this post helpful!

Okay