This is the Laravel package to extend tymon/jwt-auth.
- Disable the
QueryString
,InputSource
andRouteParams
parsers - Made the
AuthHeaders
parser optional (Default: disabled) - Prefer the
Cookies
parser - Made the cookie name changeable
- Add
AuthResource
(with cookie) - Add
RefreshJwtToken
middleware (with cookie)
$ composer require imunew/tymon-jwt-auth
vendor:publish
$ php artisan vendor:publish --provider="Imunew\JWTAuth\Providers\ServiceProvider"
Set JWT_AUTH_COOKIE_KEY
environment variable to change the cookie name.
JWT_AUTH_COOKIE_KEY={cookie name here}
Set JWT_AUTH_AUTH_HEADER_ENABLED
environment variable to enable the AuthHeaders
parser.
JWT_AUTH_AUTH_HEADER_ENABLED=true
Returning AuthResource
sets JWT token in cookie.
// App\Http\Controllers\Auth\Login
public function __invoke(LoginRequest $request)
{
$credentials = $request->only(['login_id', 'password']);
if (! $token = $this->jwtGuard->attempt($credentials)) {
throw new AuthenticationException();
}
return new AuthResource(new JwtToken($token, $this->factory->getTTL() * 60));
}
Add \Imunew\JWTAuth\Middleware\RefreshJwtToken::class
before \App\Http\Middleware\Authenticate::class
.
// App\Http\Kernel
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Imunew\JWTAuth\Middleware\RefreshJwtToken::class,
\App\Http\Middleware\Authenticate::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
Add \Imunew\JWTAuth\Middleware\RefreshJwtToken::class
to $middlewareGroups
.
// App\Http\Kernel
protected $middlewareGroups = [
'web' => [
// ...
],
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Imunew\JWTAuth\Middleware\RefreshJwtToken::class,
]
];