A RESTful API built with Laravel that demonstrates secure user authentication using JSON Web Tokens (JWT). This project provides endpoints for user registration, login, accessing protected routes, and logout using token-based authentication.
- User Registration
- Secure Login using JWT
- Token-based Authentication
- Protected API Routes
- User Profile Access
- Logout (Token Invalidation)
- RESTful API Architecture
- Laravel
- PHP
- MySQL
- JWT Authentication (tymon/jwt-auth)
- REST API
- Postman for API Testing
app/ └── Http/ └── Controllers/ └── API/ └── AuthController.php
routes/ └── api.php
database/ └── migrations/
config/ └── auth.php
- Clone Repository
git clone https://github.com/MeghaGDev/laravel-jwt-auth-api.git
- Move to Project Directory
cd laravel-jwt-auth-api
- Install Dependencies
composer install
- Copy Environment File
cp .env.example .env
- Generate Application Key
php artisan key:generate
- Generate JWT Secret Key
php artisan jwt:secret
- Configure Database
Update the .env file:
DB_DATABASE=jwt_auth_api DB_USERNAME=root DB_PASSWORD=
- Run Migrations
php artisan migrate
- Start Development Server
php artisan serve
Server will start at: http://127.0.0.1:8000
Register User
POST /api/register
Request Body: { "name": "John Doe", "email": "john@example.com", "password": "123456" }
Login
POST /api/login
Request Body: { "email": "john@example.com", "password": "123456" }
Response: { "token": "JWT_TOKEN_HERE" }
Get Authenticated User Profile
GET /api/profile
Header: Authorization: Bearer YOUR_TOKEN
Logout
POST /api/logout
Header: Authorization: Bearer YOUR_TOKEN
- User registers using the register API
- User logs in using the login API
- Server generates a JWT token
- Client sends the token in Authorization header
- Middleware verifies the token
- Protected APIs become accessible
Example Header:
Authorization: Bearer YOUR_JWT_TOKEN
You can test APIs using:
- Postman
- Insomnia
- Curl
Make sure to include the JWT token in Authorization header when accessing protected routes.
This project demonstrates:
- JWT Authentication in Laravel
- Secure API development
- Middleware-based route protection
- Token-based authentication
- RESTful API architecture
Megha
GitHub: https://github.com/MeghaGDev
This project is open-source and available under the MIT License.