Skip to content

This is the Laravel package to extend "tymon/jwt-auth".

License

Notifications You must be signed in to change notification settings

imunew/tymon-jwt-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

imunew/tymon-jwt-auth

CircleCI
This is the Laravel package to extend tymon/jwt-auth.

Extended what?

  • Disable the QueryString, InputSource and RouteParams 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)

Install

$ composer require imunew/tymon-jwt-auth

vendor:publish

$ php artisan vendor:publish --provider="Imunew\JWTAuth\Providers\ServiceProvider"

Config

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

Use AuthResource

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

Use RefreshJwtToken

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,
        ]
    ];

About

This is the Laravel package to extend "tymon/jwt-auth".

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages