Skip to content

MeghaGDev/laravel-jwt-auth-api

Repository files navigation

Laravel JWT Authentication API

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.


Features

  • User Registration
  • Secure Login using JWT
  • Token-based Authentication
  • Protected API Routes
  • User Profile Access
  • Logout (Token Invalidation)
  • RESTful API Architecture

Tech Stack

  • Laravel
  • PHP
  • MySQL
  • JWT Authentication (tymon/jwt-auth)
  • REST API
  • Postman for API Testing

Project Structure

app/ └── Http/ └── Controllers/ └── API/ └── AuthController.php

routes/ └── api.php

database/ └── migrations/

config/ └── auth.php


Installation & Setup

  1. Clone Repository

git clone https://github.com/MeghaGDev/laravel-jwt-auth-api.git

  1. Move to Project Directory

cd laravel-jwt-auth-api

  1. Install Dependencies

composer install

  1. Copy Environment File

cp .env.example .env

  1. Generate Application Key

php artisan key:generate

  1. Generate JWT Secret Key

php artisan jwt:secret

  1. Configure Database

Update the .env file:

DB_DATABASE=jwt_auth_api DB_USERNAME=root DB_PASSWORD=

  1. Run Migrations

php artisan migrate

  1. Start Development Server

php artisan serve

Server will start at: http://127.0.0.1:8000


API Endpoints

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


Authentication Flow

  1. User registers using the register API
  2. User logs in using the login API
  3. Server generates a JWT token
  4. Client sends the token in Authorization header
  5. Middleware verifies the token
  6. Protected APIs become accessible

Example Header:

Authorization: Bearer YOUR_JWT_TOKEN


API Testing

You can test APIs using:

  • Postman
  • Insomnia
  • Curl

Make sure to include the JWT token in Authorization header when accessing protected routes.


Learning Outcomes

This project demonstrates:

  • JWT Authentication in Laravel
  • Secure API development
  • Middleware-based route protection
  • Token-based authentication
  • RESTful API architecture

Author

Megha
GitHub: https://github.com/MeghaGDev


License

This project is open-source and available under the MIT License.

About

JWT Authentication REST API built with Laravel.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages