jwt-auth
jwt-auth copied to clipboard
Different TTL configurations for each guard
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,
],
],
You can create a PR to help improve that.
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 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 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.
@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
Thanks! I did not know that. I thought I was the only one having this issue.