Laravel API Authentication Comparison
This repository demonstrates and compares three popular authentication methods in Laravel for building secure APIs:
- Sanctum
- Passport
- JWT (tymon/jwt-auth)
The goal of this project is to practice, implement, and evaluate different authentication methods available in Laravel. Each method is developed in a separate Git branch for clarity and easy comparison.
This project serves as a learning opportunity and showcases my ability to:
- Work with Laravel 12 and modern tools
- Implement secure and RESTful authentication APIs
- Handle JSON-based request validation and error responses
- Follow best practices for structuring Laravel API projects
Each branch represents one method of authentication:
sanctum-auth
— Laravel Sanctum implementationpassport-auth
— Laravel Passport implementationjwt-auth
— JWT (tymon/jwt-auth) implementation
- Basic user CRUD operations:
- Create a new user
- View all users
- View a specific user
- Update user
- Delete user
- Server-side validation with JSON error responses
- Token-based authentication (per method)
- Clean, modular code
- Comments and documentation in code
- Laravel 12
- Composer
- Git & Git branches
- Laravel Sanctum / Passport / JWT
- PHP 8.2+
- Clone the repository and switch to the desired branch:
git clone https://github.com/akram-khodami/laravel-auth-showcase.git
cd laravel-auth-showcase
git checkout sanctum-auth # or passport-auth / jwt-auth
- Install dependencies:
composer install
- Set up your .env file:
- php artisan key:generate
+ cp .env.example .env
+ php artisan key:generate
- Set up your database:
- Create a new MySQL (or other) database, e.g.
laravel_auth_showcase
- Open
.env
file and update the following:
DB_DATABASE=laravel_auth_showcase
DB_USERNAME=your_db_username
DB_PASSWORD=your_db_password
- Run migrations:
php artisan migrate
- Seed test data(Optional) :
php artisan db:seed
- Start the development server:
php artisan serve
- If you are working with the
auth-passport
branch, make sure to run the following commands to generate a personal access client and clear the cache:
php artisan passport:client --personal
php artisan cache:clear
This project is open-source and available under the MIT license.
Feel free to fork, contribute, or use it as a base for your own Laravel API projects.