Skip to content

Repository files navigation

Laravel API Authentication using Sanctum, Passport, and JWT

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.

Branches

  • 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).

Getting Started

Follow these steps to set up and use the repository:

Step 1: Clone the Repository

Clone the repository to your local machine using the following command:

git clone https://github.com/cdmathukiya/laravel-REST-API-crud-example.git

Step 2: Navigate to the Project Directory

cd laravel-REST-API-crud-example

Step 3: Choose Your Authentication Method

Depending 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

    Install Dependencies

    composer install
  • Option 2: Passport

    If you want to use Laravel Passport, checkout the passport branch:

    git checkout passport

    Install Dependencies

    composer install

    Generate Passport keys

    php artisan passport:keys
  • Option 3: JWT

    If you want to use JWT (JSON Web Tokens), checkout the jwt branch:

    git checkout jwt

    Install Dependencies

    composer install

    Generate Secret keys

    php artisan jwt:secret

    Note: Setup .env environment file before execute this command

Step 4: Set Up Environment Variables

Copy the .env.example file to .env and update the necessary environment variables (e.g., database credentials):

cp .env.example .env

Generate an application key:

php artisan key:generate

Step 5: Run Migrations

Run the database migrations to set up the required tables:

php artisan migrate

Step 6: Start the Development Server

Start the Laravel development server:

php artisan serve

Visit http://localhost:8000 in your browser to access the application.


Authentication Methods Overview

1. Sanctum

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.

2. Passport

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.

3. JWT

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.


Contributing

If you'd like to contribute to this project, feel free to open a pull request or create an issue. Contributions are welcome!

License

This project is open-source and available for every one.


Happy coding! 🚀

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages