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

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

Open
jfoza opened this issue Apr 7, 2024 · 2 comments
Open

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

jfoza opened this issue Apr 7, 2024 · 2 comments

Comments

@jfoza
Copy link

jfoza commented Apr 7, 2024

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
/**
* Get the Expiration (exp) claim.
*
* @return int
*/
public function exp()
{
return Utils::now()->addMinutes($this->ttl)->getTimestamp();
}

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
Copy link

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!

@jfoza
Copy link
Author

jfoza commented Sep 4, 2024

In composer.json

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
        "exclude-from-classmap" : [
            "vendor/tymon/jwt-auth/src/Claims/Factory.php"
        ],
        "files" : [
            "overrides/tymon/jwt-auth/src/Claims/Factory.php"
        ]
    },

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

No branches or pull requests

2 participants