think.studio/laravel-jwt-auth

Another laravel jwt auth package.

3.3.0 2023-09-10 07:39 UTC

This package is auto-updated.

Last update: 2024-12-10 11:03:31 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

Another laravel jwt auth package.
This package has very slow support, you might be better off switching to an older and more used package: tymon/jwt-auth

Installation

Install the package via composer:

composer require think.studio/laravel-jwt-auth

You can publish the config file with:

php artisan vendor:publish --provider="JWTAuth\ServiceProvider" --tag="config"

You can publish migrations:

php artisan vendor:publish --provider="JWTAuth\ServiceProvider" --tag="migrations"

If you don't have encryption / decryption keys, generate them using the command

php artisan jwt:keys:generate

Configuration

Update auth configuration

// config/auth.php
  'guards' => [
        // ...
        'my_api_guard_name' => [
            'driver'      => 'jwt',
            'provider'    => 'users',
            'public_key'  => env('JWT_PUBLIC_KEY', 'jwt-keys/jwtRS256.key.pub'),
            'private_key' => env('JWT_PUBLIC_KEY', 'jwt-keys/jwtRS256.key'),
            'blocklist'   => 'filesystem',
            'options'     => [],
        ],
    ],

Update User model

use Illuminate\Foundation\Auth\User as Authenticatable;
use JWTAuth\Contracts\WithJwtToken;

class User extends Authenticatable implements WithJwtToken
{
    use \JWTAuth\Eloquent\HasJwtToken;
    //...
}

Usage

Login

/** @var \JWTAuth\JWTGuard $auth */
$auth = Auth::guard('my_api_guard_name');
$token = $auth->attempt($request->only( 'email', 'password'));
if ($token) {
    $user = $auth->user();
    echo "Access token: {$token}";
    echo "User id: {$user->id}";
}

Logout

if(Auth::guard('my_api_guard_name')->check()) {
    Auth::guard('my_api_guard_name')->logout();
}

Credits

  • Think Studio