Search code examples
phplaravelaccess-tokenlaravel-passport

Laravel Passport Token Expire Methods Not Working


I use passport to make authentication for my API's i run this command to install passport :

php artisan passport:install --force

and use the following codes to generate token :

$objToken = $user->createToken('Token');
$strToken = $objToken->accessToken;
$expiration = $objToken->token->expires_at->diffForHumans();

return response()->json([
     token' => $strToken,
     'ExpireTime' => $expiration,
], 200);

i found that my token lifetime is one year, i just want to make the expire_at column to 1 hour i read official document and add following codes to AuthServiceProvider:

Passport::tokensExpireIn(now()->addDays(15));
Passport::refreshTokensExpireIn(now()->addDays(30));

but those codes not working and when i log the expire_at , this still be one year please help me to change expiration time of my tokens. thakns alot :)


Solution

  • You are trying to create Personal Access Token.

    // Passport::tokensExpireIn(now()->addDays(15));
    // Passport::refreshTokensExpireIn(now()->addDays(30));
    
    
    # Get or set when personal access tokens expire.
    Passport::personalAccessTokensExpireIn(now()->addHour(1));
    

    Result :

    array:2 [
      "token" => "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...."
      "ExpireTime" => "59 minutes from now"
    ]
    

    Update

    For Laravel < v5.7

    Personal access tokens are always long-lived. Their lifetime is not modified when using the tokensExpireIn or refreshTokensExpireIn methods.