Skip to content

developerabdan/auth-api-laravel

Repository files navigation

Laravel Logo

Latest Stable Version License

Laravel Authentication API with Sanctum

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.

Overview

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

Architecture

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)

API Endpoints

Authentication

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

Request Examples

Register a New User

POST /api/register
Content-Type: application/json

{
  "name": "John Doe",
  "email": "[email protected]",
  "password": "password123"
}

Login

POST /api/login
Content-Type: application/json

{
  "email": "[email protected]",
  "password": "password123"
}

Get User Profile

GET /api/user
Authorization: Bearer {your_access_token}

Testing

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

Postman Collection

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.

Installation

  1. Clone the repository
  2. Install dependencies:
    composer install
  3. Configure environment:
    cp .env.example .env
    php artisan key:generate
  4. Run migrations:
    php artisan migrate
  5. Start the server:
    php artisan serve

Technology Stack

  • Laravel: PHP framework for web development
  • Sanctum: Laravel's lightweight authentication system
  • PHPUnit: Testing framework default by Laravel

License

This project is open-sourced software licensed under the MIT license.

Author

@abdansyakuro.id

About

Example of Authentication API Laravel using Sanctum Built-in

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages