kamrul-haque/laravel-passport-auth

Authentication functionality for API using Laravel Passport

v1.0.0 2022-12-28 19:31 UTC

This package is auto-updated.

Last update: 2024-04-28 22:50:49 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License GitHub Repo stars

Authentication functionality for API using Laravel Passport

Prerequisite

Install and configure Laravel Passpost

Installation

Install the package via composer:

composer require kamrul-haque/laravel-passport-auth

Publish package resources:

php artisan vendor:publish --tag="laravel-passport-auth"

Migrate the necessary database tables:

php artisan migrate

Configuration

Set the required configuration in .env.

// .env

FRONTEND_URL=
FRONTEND_IP=
TOKEN_EXPIRY_DAYS=
TOKEN_REFRESH_EXPIRY_DAYS=

Usage:

Add routes in api.php:

// routes/api.php

include __DIR__ . '/passport.php';

Protect routes:

// routes/api.php

Route::group(['middleware' => 'auth:api'], function () {
  // your routes
});

Register middleware in app\Http\Kernel.php inside $routeMiddleware array if you want to restrict API calls from certain IP address:

// app\Http\Kernel.php

protected $routeMiddleware = [
  // existing middlewares

  'restrict-request-ip' => \App\Http\Middleware\RestrictRequestIP::class,
];

Assign the middleware to routes:

// routes/api.php

Route::group(['middleware' => 'restrict-request-ip'], function () {
  // your routes
});