cesg/jwt-token-guard

Laravel简单JWT token守卫

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

This package is auto-updated.

Last update: 2024-09-25 15:34:48 UTC


README

Laravel简单JWT token守卫

安装

composer require cesg/jwt-token-guard

配置

配置认证驱动

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

示例密钥

openssl rand -hex 64

用法

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'));
}