Amirreza Moradi
Amirreza Moradi

Reputation: 231

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 :)

Upvotes: 2

Views: 5706

Answers (2)

MohammadReza
MohammadReza

Reputation: 19

$objToken = $user->createToken('Token');
$strToken = $objToken->accessToken;
$expiration = $objToken->token;
$expiration->expires_at = Carbon::now()->addWeeks(1);//example 1 week

$expiration->save();

and in AuthServiceProvider.php:

public function boot()
{
    $this->registerPolicies();
    Passport::routes();
    Passport::personalAccessTokensExpireIn(Carbon::now()->addDays(2));//example 2 Days
    //
}

Upvotes: 0

W Kristianto
W Kristianto

Reputation: 9303

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.

Upvotes: 2

Related Questions