tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.32k stars 1.54k forks source link

Method factory on JWTGuard not found #2232

Closed kingkero closed 9 months ago

kingkero commented 1 year ago

Subject of the issue

Running static analysis such as PHPStan results in an error for using $guard->factory() even if $guard instanceof JWTGuard is true.

'expires_in' => auth()->factory()->getTTL() * 60

this doesn't pass PHPStan with the error message

Call to an undefined method                                                 
     Illuminate\Contracts\Auth\StatefulGuard&Tymon\JWTAuth\JWTGuard::factory().

Your environment

Q A
Bug? no / yes
New Feature? no
Framework Laravel
Framework version 10.29
Package version 2.0.0
PHP version 8.2

Steps to reproduce

In a Controller that uses the api:auth middleware, using

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Symfony\Component\Process\Exception\RuntimeException;
use Tymon\JWTAuth\JWTGuard;

class RefreshController extends Controller
{
    public function __invoke(): JsonResponse
    {
        $guard = Auth('api');
        if (! $guard instanceof JWTGuard) {
            throw new RuntimeException('Wrong guard returned.');
        }

        return response()->json([
            'token_type' => 'Bearer',
            'access_token' => $guard->refresh(),
            'expires_in' => $guard->factory()->getTTL() * 60,
        ]);
    }
}

Expected behaviour

PHPStan should realize that ->factory() exists on the JWTGuard (via the jwt token)

Actual behaviour

Triggers the

Call to an undefined method Illuminate\Contracts\Auth\StatefulGuard&Tymon\JWTAuth\JWTGuard::factory().

error