tymondesigns / jwt-auth

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

Error when using the auth()->tokenById($id) function. #2253

Open jfoza opened 3 months ago

jfoza commented 3 months ago

Subject of the issue

When using the tokenById function, the error Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given, called in \/var\/www\/html\/vendor \/nesbot\/carbon\/src\/Carbon\/Traits\/Units.php on line 356 is fired. The problem occurred with the variable JWT_TTL which is arriving as a string instead of an integer when this function is called /**

This change resolved the issue in an alternative way: $ttl = (int) $this->ttl; return Utils::now()->addMinutes($ttl)->getTimestamp();

Your environment

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 11.2.0
Package version 2.1.1
PHP version 8.2.16

Steps to reproduce

Perform a simple authentication using the auth()->tokenById($id) function.

Krixx1337 commented 2 months ago

I encountered a similar issue with the Carbon\Carbon::rawAddUnit() error due to the JWT ttl value being passed as a string. I managed to resolve this by ensuring that the ttl value is cast to an integer directly in the configuration file. This might help anyone else experiencing this error.

Here’s how I modified the configuration:

// In config/jwt.php
'ttl' => (int) env('JWT_TTL', 60),  // Cast to int here

This change ensures that the ttl value is always an integer, regardless of how it's specified in the environment variables. It might be helpful to include this type of casting within the library itself to prevent similar issues.

Hope this helps!