This repository demonstrates how to implement API authentication in Laravel using three different methods: Sanctum, Passport, and JWT. Each method is implemented in a separate branch for easy reference and usage.
master: The main branch containing the base Laravel setup and documentation.sanctum: Implementation of API authentication using Laravel Sanctum.passport: Implementation of API authentication using Laravel Passport.jwt: Implementation of API authentication using JWT (JSON Web Tokens).
Follow these steps to set up and use the repository:
Clone the repository to your local machine using the following command:
git clone https://github.com/cdmathukiya/laravel-REST-API-crud-example.gitcd laravel-REST-API-crud-exampleDepending on your preferred authentication method, switch to the corresponding branch:
-
Option 1: Sanctum
If you want to use Laravel Sanctum, checkout the sanctum branch:
git checkout sanctum
composer install
-
Option 2: Passport
If you want to use Laravel Passport, checkout the passport branch:
git checkout passport
composer install
php artisan passport:keys
-
Option 3: JWT
If you want to use JWT (JSON Web Tokens), checkout the jwt branch:
git checkout jwt
composer install
php artisan jwt:secret
Note: Setup
.envenvironment file before execute this command
Copy the .env.example file to .env and update the necessary environment variables (e.g., database credentials):
cp .env.example .envGenerate an application key:
php artisan key:generateRun the database migrations to set up the required tables:
php artisan migrateStart the Laravel development server:
php artisan serveVisit http://localhost:8000 in your browser to access the application.
Laravel Sanctum provides a lightweight authentication system for SPAs (Single Page Applications), mobile applications, and simple token-based APIs. It is easy to set up and ideal for projects that require simple authentication.
Laravel Passport is a full OAuth2 server implementation for API authentication. It is suitable for applications that require OAuth2 functionality, such as third-party API integrations.
JWT (JSON Web Tokens) is a token-based authentication method that is widely used in modern APIs. It is stateless and works well for distributed systems.
If you'd like to contribute to this project, feel free to open a pull request or create an issue. Contributions are welcome!
This project is open-source and available for every one.
Happy coding! 🚀