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

How to set lifetime expiration time of passport access token in 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.

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

How to Hire Laravel Developers and How Much Does It Cost.

Want to make the most out of the latest Laravel technology? Here is your guide on how to hire the best Laravel developer, and at the right price. 

How to perform migration rollback in laravel

Sometimes we need to rollback one specific migration. Here, in this article we will discuss about the migration rollback in laravel.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→