How to set lifetime expiration time of passport access token in Laravel

Originally published by Hardik Savani at https://itsolutionstuff.com

We can increase token expire time of access token using tokensExpireIn(). 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-&gt;registerPolicies();

    Passport::routes();
    Passport::tokensExpireIn(now()-&gt;addDays(30));
    Passport::refreshTokensExpireIn(now()-&gt;addDays(30));
    Passport::personalAccessTokensExpireIn(now()-&gt;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-&gt;registerPolicies();

    Passport::routes();
    Passport::tokensExpireIn(now()-&gt;addYears(5));
    Passport::refreshTokensExpireIn(now()-&gt;addYears(5));
    Passport::personalAccessTokensExpireIn(now()-&gt;addYears(5));
}

}

I hope it can help you…

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

PHP with Laravel for beginners - Become a Master in Laravel

Projects in Laravel: Learn Laravel Building 10 Projects

Laravel for RESTful: Build Your RESTful API with Laravel

Fullstack Web Development With Laravel and Vue.js

Laravel 5.8 Tutorial for Beginners

Laravel 5.8 Ajax CRUD tutorial using Datatable JS

Laravel 5.8 Tutorial from Scratch for Beginners

Build RESTful API In Laravel 5.8 Example

Login with Google in Laravel 5.8 App using Socialite Package

Laravel PHP Framework Tutorial - Full Course for Beginners (2019)

#laravel

How to set lifetime expiration time of passport access token in Laravel
155.60 GEEK