jwt-auth icon indicating copy to clipboard operation
jwt-auth copied to clipboard

Different TTL configurations for each guard

Open avidianity opened this issue 2 years ago • 1 comments

Summary

I have the following guards defined which uses different models and it works really well for me, one problem that I have is I can't have them configured with their own set of TTLs. I can do it outside the config but it's hard to maintain.

I have defined my auth.php config like this:

    'guards' => [
        'administrators' => [
            'driver' => 'jwt',
            'provider' => 'administrators',
        ],
        'customers' => [
            'driver' => 'jwt',
            'provider' => 'customers',
        ],
        'clinics' => [
            'driver' => 'jwt',
            'provider' => 'clinics',
        ],
        'patients' => [
            'driver' => 'jwt',
            'provider' => 'patients',
        ],
    ],

    'providers' => [
        'administrators' => [
            'driver' => 'eloquent',
            'model' => App\Models\Administrator::class,
        ],
        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Models\Customer::class,
        ],
        'clinics' => [
            'driver' => 'eloquent',
            'model' => App\Models\Clinic::class,
        ],
        'patients' => [
            'driver' => 'eloquent',
            'model' => App\Models\Patient::class,
        ],
    ],

It would be nice if I could do the following:

    'guards' => [
        'administrators' => [
            'driver' => 'jwt',
            'provider' => 'administrators',
            'ttl' => 2880,
        ],
        'customers' => [
            'driver' => 'jwt',
            'provider' => 'customers',
            'ttl' => 1440,
        ],
        'clinics' => [
            'driver' => 'jwt',
            'provider' => 'clinics',
            'ttl' => 60,
        ],
        'patients' => [
            'driver' => 'jwt',
            'provider' => 'patients',
            'ttl' => 60,
        ],
    ],

    'providers' => [
        'administrators' => [
            'driver' => 'eloquent',
            'model' => App\Models\Administrator::class,
        ],
        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Models\Customer::class,
        ],
        'clinics' => [
            'driver' => 'eloquent',
            'model' => App\Models\Clinic::class,
        ],
        'patients' => [
            'driver' => 'eloquent',
            'model' => App\Models\Patient::class,
        ],
    ],

avidianity avatar Jul 14 '22 09:07 avidianity

You can create a PR to help improve that.

Messhias avatar Jul 29 '22 08:07 Messhias

I can do it outside the config but it's hard to maintain.

Hello @avidianity. How do you do it outside the config? When I try out the $token = auth()->setTTL(120)->attempt($credentials); in the docs, it's saying that undefined method setTTL

xyl-san avatar Jul 29 '23 08:07 xyl-san

@xyl-san auth() is probably defaulting to the web guard if you don't pass in the guard argument. You need to change your default guard in config/auth.php or pass in your jwt guard inside the auth() function ex: auth('api').

avidianity avatar Jul 29 '23 11:07 avidianity

@xyl-san auth() is probably defaulting to the web guard if you don't pass in the guard argument. You need to change your default guard in config/auth.php or pass in your jwt guard inside the auth() function ex: auth('api').

Yes. I passed my custom guard in the api() method. But VS Code is still saying that its an undefined method. I tried sending a request, and to my surprise it actually gave me a valid token. So I think it's VSCode that has a problem.

image

xyl-san avatar Jul 31 '23 05:07 xyl-san

@xyl-san that's a common issue in vscode, I have that as well. Installing jwt-auth or other 3rd party auth packages does not modify auth()'s internal typings but regardless of that intellisense issue you're having it will still work if you run it.

What I do in my case is use a typed property or add it's correct type using phpdoc.

Solution 1:

use PHPOpenSourceSaver\JWTAuth\JWTGuard;

class AuthController extends Controller
{
    protected JWTGuard $guard;

    public function __construct()
    {
        $this->guard = auth('api');
    }
}

Solution 2:

use PHPOpenSourceSaver\JWTAuth\JWTGuard;

class AuthController extends Controller
{
    public function yourMethod()
    {
        /**
         * @var JWTGuard
         */
        $guard = auth('api');
    }
}

there's also a way to modify it's internal typings as well by using a method similar to barryvdh/laravel-ide-helper

avidianity avatar Jul 31 '23 09:07 avidianity

Thanks! I did not know that. I thought I was the only one having this issue.

xyl-san avatar Aug 01 '23 02:08 xyl-san