Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different TTL configurations for each guard #164

Closed
avidianity opened this issue Jul 14, 2022 · 6 comments · Fixed by #223
Closed

Different TTL configurations for each guard #164

avidianity opened this issue Jul 14, 2022 · 6 comments · Fixed by #223
Labels
enhancement New feature or request

Comments

@avidianity
Copy link
Contributor

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 avidianity added the enhancement New feature or request label Jul 14, 2022
@Messhias
Copy link
Collaborator

You can create a PR to help improve that.

@xyl-san
Copy link

xyl-san commented Jul 29, 2023

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

@avidianity
Copy link
Contributor Author

@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').

@xyl-san
Copy link

xyl-san commented Jul 31, 2023

@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

@avidianity
Copy link
Contributor Author

@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

@xyl-san
Copy link

xyl-san commented Aug 1, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants