Home > Blockchain >  Laravel Error: Call to a member function isPast() on string laravel/sanctum/src/Guard.php on line 16
Laravel Error: Call to a member function isPast() on string laravel/sanctum/src/Guard.php on line 16

Time:01-29

I'm just configured laravel project with sanctum.

And trying to check is ok, making request to /api/user:

Route::post('/login', [AuthController::class, 'login']);

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

But facing this error:

Error: Call to a member function isPast() on string in file /var/www/TiWake/vendor/laravel/sanctum/src/Guard.php on line 160

#0 /var/www/TiWake/vendor/laravel/sanctum/src/Guard.php(69): Laravel\Sanctum\Guard->isValidAccessToken(Object(App\Models\PersonalAccessToken))
#1 [internal function]: Laravel\Sanctum\Guard->__invoke(Object(Illuminate\Http\Request), NULL)
#2 /var/www/TiWake/vendor/laravel/framework/src/Illuminate/Auth/RequestGuard.php(57): call_user_func(Object(Laravel\Sanctum\Guard), Object(Illuminate\Http\Request), NULL)
#3 /var/www/TiWake/vendor/laravel/framework/src/Illuminate/Auth/GuardHelpers.php(60): Illuminate\Auth\RequestGuard->user()
#4 /var/www/TiWake/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(63): Illuminate\Auth\RequestGuard->check()
#5 /var/www/TiWake/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(42): Illuminate\Auth\Middleware\Authenticate->authenticate(Object(Illuminate\Http\Request), Array)

after that I trying to use custom PersonalAccessToken model with casts:

class PersonalAccessToken extends SanctumPersonalAccessToken
{

    protected $casts = [    
        'abilities' => 'json',
        'last_used_at' => 'datetime',
        'expired_at' => 'datetime',
        '   ' => 'datetime',
    ];

Then in AppServiceProvider:

public function boot()
    {
        Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
    }

Source code of laravel/sanctum/src/Guard.php is:

protected function isValidAccessToken($accessToken): bool
    {
        if (! $accessToken) {
            return false;
        }

        $isValid =
            (! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration)))

// Error is here && (! $accessToken->expires_at || ! $accessToken->expires_at->isPast()) && $this->hasValidProvider($accessToken->tokenable);

        if (is_callable(Sanctum::$accessTokenAuthenticationCallback)) {
            $isValid = (bool) (Sanctum::$accessTokenAuthenticationCallback)($accessToken, $isValid);
        }

        return $isValid;
    }

But error still staying.

"laravel/framework": "^9.0"
"laravel/sanctum": "^3.2"

CodePudding user response:

Could it be that you're casting expired_at to datetime but the real name is expires_at?

  • Related