cesg/jwt-token-guard

Laravel simple JWT token guard

v0.5.0 2023-01-25 11:43 UTC

This package is auto-updated.

Last update: 2024-03-25 14:16:30 UTC


README

Laravel simple JWT token guard

Install

composer require cesg/jwt-token-guard

Configure

Configure the auth driver

'api' => [
    'driver' => 'jwt',
    'provider' => 'users',
    'key' => env('JWT_KEY', \md5(env('APP_NAME'))),
],

Example secret key

openssl rand -hex 64

Usage

Javascript

const token = '';
axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;

Laravel

protected function authenticated(Request $request, $user)
{
    $jwt = JWT::encode([
        'sub' => $user->getAuthIdentifier(),
        'iss' => config('app.name'),
        'iat' => now()->timestamp,
    ], config('auth.guards.api.key'));

    session(\compact('jwt'));
}