A robust, secure, and well-tested Laravel API authentication system using Laravel Sanctum for token-based authentication. This project implements SOLID principles for clean, maintainable code architecture.
This project provides a complete authentication API solution built with Laravel, featuring:
- Token-based Authentication using Laravel Sanctum
- SOLID Principle Implementation for maintainable architecture
- Comprehensive Test Coverage for all endpoints
- Clean, RESTful API Design
The project follows SOLID principles for clean code and maintainability:
- Single Responsibility Principle: Each class has one responsibility (e.g.,
SanctumServices
handles only authentication logic) - Open/Closed Principle: Architecture allows extending functionality without modifying existing code
- Liskov Substitution Principle: Service implementations can be substituted without affecting behavior
- Interface Segregation Principle: Focused interfaces like
AuthServiceInterface
define clear contracts - Dependency Inversion Principle: High-level modules depend on abstractions (e.g., controllers depend on interfaces)
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /api/register |
Register a new user | No |
POST | /api/login |
Login and get access token | No |
GET | /api/user |
Get authenticated user details | Yes |
POST /api/register
Content-Type: application/json
{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}
POST /api/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "password123"
}
GET /api/user
Authorization: Bearer {your_access_token}
The application includes comprehensive test coverage for all API endpoints:
- Registration success and failure scenarios
- Authentication validation
- Access control enforcement
- Input validation
Run the tests with:
php artisan test
A Postman collection is available to easily test all API endpoints:
Auth API Laravel Postman Collection
Import this collection to quickly test the authentication flow.
- Clone the repository
- Install dependencies:
composer install
- Configure environment:
cp .env.example .env php artisan key:generate
- Run migrations:
php artisan migrate
- Start the server:
php artisan serve
- Laravel: PHP framework for web development
- Sanctum: Laravel's lightweight authentication system
- PHPUnit: Testing framework default by Laravel
This project is open-sourced software licensed under the MIT license.
@abdansyakuro.id