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.
- 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
.
- PHP >= 7.4
- Composer
- SQLite
-
Clone the repository:
git clone https://github.com/testsmith-io/api-demo.git cd api-demo
-
Install dependencies:
composer install
-
Run migrations:
php artisan migrate:fresh
- tymon/jwt-auth: Handles JWT-based authentication.
- darkaonline/l5-swagger: Generates Swagger documentation based on OpenAPI specification.
To start the Laravel development server, run:
php artisan serve
The API will be accessible at http://localhost:8000
.
- 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."] }
- 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" }
To access protected routes, include the JWT token in the Authorization header:
Authorization: Bearer <token>
- URL:
GET /api/users
- Description: Retrieves a list of users. Only accessible with a valid JWT token.
Swagger documentation is automatically generated for this API. To view it, go to:
http://localhost:8000/api/documentation
If you make changes to the API and need to regenerate the Swagger docs:
php artisan l5-swagger:generate
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.
If you want to list all available routes, use the following command:
php artisan route:list