Skip to content

testsmith-io/api-demo

Repository files navigation

Demo API with JWT Authentication and Swagger Documentation

A RESTful API built with Laravel for Demo purpose, secured with JSON Web Tokens (JWT) and documented using Swagger. This API uses a SQLite database for quick and easy development.

Features

  • User Registration and Login: Supports creating new users and authenticating them via JWT.
  • JWT Authentication: Secures protected endpoints with JSON Web Tokens.
  • Swagger Documentation: Generates interactive API documentation using OpenAPI with darkaonline/l5-swagger.

Prerequisites

  • PHP >= 7.4
  • Composer
  • SQLite

Installation

  1. Clone the repository:

    git clone https://github.com/testsmith-io/api-demo.git
    cd api-demo
  2. Install dependencies:

    composer install
  3. Run migrations:

    php artisan migrate:fresh

Packages Used

Running the API

To start the Laravel development server, run:

php artisan serve

The API will be accessible at http://localhost:8000.

Endpoints

User Registration

  • URL: POST /api/register
  • Description: Registers a new user.
  • Request Body:
    {
      "name": "John Doe",
      "email": "johndoe@example.com",
      "password": "password123"
    }
  • Response:
    • 201 Created:
      {
        "message": "User successfully registered"
      }
    • 422 Unprocessable Entity (Validation errors):
      {
        "name": ["The name field is required."],
        "email": ["The email has already been taken."]
      }

User Login

  • URL: POST /api/login
  • Description: Authenticates a user and returns a JWT token.
  • Request Body:
    {
      "email": "johndoe@example.com",
      "password": "password123"
    }
  • Response:
    • 200 OK:
      {
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
      }
    • 401 Unauthorized:
      {
        "error": "Unauthorized"
      }

JWT Authentication

To access protected routes, include the JWT token in the Authorization header:

Authorization: Bearer <token>

Example Protected Route (User List)

  • URL: GET /api/users
  • Description: Retrieves a list of users. Only accessible with a valid JWT token.

Swagger Documentation

Swagger documentation is automatically generated for this API. To view it, go to:

http://localhost:8000/api/documentation

Generating or Updating Swagger Documentation

If you make changes to the API and need to regenerate the Swagger docs:

php artisan l5-swagger:generate

Project Structure

  • app/Http/Controllers/UserController.php: Contains logic for user registration and login.
  • app/Http/Requests/RegisterUserRequest.php: Handles validation for user registration.
  • routes/api.php: Defines API routes for registration, login, and protected routes.
  • config/l5-swagger.php: Swagger configuration for documentation generation.

List all routes

If you want to list all available routes, use the following command:

php artisan route:list

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages