ItSolutionStuff.com

Laravel Passport Access Token Expire Lifetime

By Hardik Savani • April 16, 2024
Laravel

In this post, we will learn how to set lifetime expiration time of passport access token in laravel. we can set personal access token expiry time longer and also event shorter using tokensExpireIn, refreshTokensExpireIn, and personalAccessTokensExpireIn methods.

we can increase token expire time of access token using tokensExpireIn() in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 app. we can increase refresh token expire time of access token using refreshTokensExpireIn(). we can increase personal access token expire time of access token using personalAccessTokensExpireIn().

Let's see bellow example to set longer time of expire access token in laravel 5 application.

Example 1: app/Provides/AuthServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider

{

/**

* The policy mappings for the application.

*

* @var array

*/

protected $policies = [

];

/**

* Register any authentication / authorization services.

*

* @return void

*/

public function boot()

{

$this->registerPolicies();

Passport::routes();

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

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

Passport::personalAccessTokensExpireIn(now()->addDays(30));

}

}

Example 2: app/Provides/AuthServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider

{

/**

* The policy mappings for the application.

*

* @var array

*/

protected $policies = [

];

/**

* Register any authentication / authorization services.

*

* @return void

*/

public function boot()

{

$this->registerPolicies();

Passport::routes();

Passport::tokensExpireIn(now()->addYears(5));

Passport::refreshTokensExpireIn(now()->addYears(5));

Passport::personalAccessTokensExpireIn(now()->addYears(5));

}

}

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 5.7 - Create REST API with authentication using Passport Tutorial

Read Now →

How to Add Charts in Laravel using Highcharts?

Read Now →

Example of unionAll in Query Builder Laravel

Read Now →

How to Create Widgets in Laravel Application?

Read Now →

How to Set URL without Http of Other Site in Laravel?

Read Now →

Laravel CKeditor 5 Image Upload Tutorial Example

Read Now →