Introduction
In modern web development, APIs are essential for communication between clients (web or mobile apps) and servers. To protect these APIs, authentication and authorization are critical. One popular method is the use of JSON Web Tokens (JWT), a compact and secure way to transmit user information.
In this tutorial, you will learn how to implement JWT-based API authentication in Laravel 12. We'll cover everything from installing Laravel, setting up the JWT package, creating authentication endpoints, and securing your routes.
What is an API?
An API (Application Programming Interface) is a set of rules that allows different software applications to communicate with each other. APIs enable the backend server to provide data and services to frontend clients, such as websites or mobile apps.
What is JWT?
JSON Web Token (JWT) is a standard for securely transmitting information as a JSON object. It is self-contained, compact, and digitally signed. JWTs are commonly used to authenticate users in web and mobile applications, allowing the server to verify a user's identity without storing session information.
Why Use JWT in Laravel?
-
Stateless authentication — no need to store session data on the server.
-
Scalable and suitable for APIs.
-
Can be used across different domains and platforms.
-
Secure transmission of user info.
Tutorial Overview
We will build a RESTful API in Laravel 12 with the following endpoints:
Endpoint | Method | Description |
---|---|---|
/api/auth/register | POST | Register a new user |
/api/auth/login | POST | User login and get JWT |
/api/auth/profile | POST | Get authenticated user info |
/api/auth/refresh | POST | Refresh the JWT token |
/api/auth/logout | POST | Logout user and invalidate token |
Step 1: Install Laravel 12
If you haven't already, install Laravel 12 via Composer:
Step 2: Enable API and Customize Authentication Exception
Laravel 12’s API routes need to be enabled. You can do this by running:
Next, update the bootstrap/app.php
file to customize the authentication exception response for API routes so unauthorized access returns a JSON response:
Step 3: Install JWT Package
Install the php-open-source-saver/jwt-auth
package:
Publish the package configuration:
Generate the JWT secret key (add to .env
):
Open .env
and set:
(Change DB settings if needed)
Run the migration command
Step 4: Configure Authentication Guard
Open config/auth.php
and update the api
guard to use jwt
driver:
Step 5: Update User Model
Edit app/Models/User.php
to implement JWTSubject:
Step 6: Create API Routes
Define API routes in routes/api.php
:
Step 7: Create AuthController
Create an API controller to handle user registration, login, logout, and token refresh.
Run:
Then, add the following code inside app/Http/Controllers/API/AuthController.php
:
Step 8: Run Laravel Server
Your API will run at http://localhost:8000
.
How to Use the API
1. Register User
-
URL:
POST /api/auth/register
-
Body:
2. Login User
-
URL:
POST /api/auth/login
-
Body:
-
Response:
3. Get Profile
-
URL:
POST /api/auth/profile
-
Headers:
Authorization: Bearer {access_token}
4. Refresh Token
-
URL:
POST /api/auth/refresh
-
Headers:
Authorization: Bearer {access_token}
5. Logout
-
URL:
POST /api/auth/logout
-
Headers:
Authorization: Bearer {access_token}
Conclusion
You have now built a secure JWT authentication API using Laravel 12! This setup is ideal for web or mobile apps that require stateless authentication.
You can extend this tutorial by adding roles, permissions, or social logins.
Want the full source code?
Download the complete Laravel 12 JWT API Authentication example on my GitHub repo here.
Happy Coding!