You have used Laravel passport to do API authentication and now you want to handle token expiration then this laravel passport set token expiration tutorial is for you. If you are facing problems like laravel passport token expiration not working, this example will give you the solution.

By default, Laravel Passport provides access tokens that expire after one year. If we want to configure a custom lifetime, we may use the tokensExpireInrefreshTokensExpireIn, and personalAccessTokensExpireIn methods.

Just we need to update our AuthServiceProvider like that:

App\Providers\AuthServiceProvider.php

<?php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The model to policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        // 'App\Models\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     */
    public function boot(): void
    {
        Passport::tokensExpireIn(now()->addDays(15));
        Passport::refreshTokensExpireIn(now()->addDays(30));
        Passport::personalAccessTokensExpireIn(now()->addMonths(6));
    }
}

 

Just update the days that you want to use to handle laravel passport token expiration.

 

Read also: Laravel 10 Passport API Authentication Tutorial

 

Conclusion

Now we know how to set token expiry time in Laravel Passport. Hope this laravel passport token expiration time tutorial will help you to handle laravel passport token expiration in your laravel application.

Category : #laravel

Tags : #laravel , #laravel api