Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

YeZawHein's avatar

Logic Architecuture for passport token expire time

I don't know that much about passport.I need advice for passport token expire time.I searched for it but I still haven't found any appropiate answer. When user click remember_me token lifetime should be long-lived. So,

//AuthServiceProvider.php

 if ($request->remember_me) {
            Passport::personalAccessTokensExpireIn(Carbon::now()->addHours(1));
        } else {
            Passport::personalAccessTokensExpireIn(Carbon::now()->addMinutes(1));
        }

It works but I think that isn't not appropiate solution.I didn't want to set this logic in AuthServiceProvider.php.I want to handle in controller.

//Controller 
if ($request->remember_me) {
        Passport::personalAccessTokensExpireIn(Carbon::now()->addHours(1));//it doesn't work

            $token->expires_at = Carbon::now()->addHours(1);//only work in database value ,it doesn't affect for passport
        }

//AuthServiceProvider
    Passport::personalAccessTokensExpireIn(Carbon::now()->addMinutes(1)); //I want to put this default.

Any your advice will be greatly appreciated.

0 likes
1 reply
YeZawHein's avatar

Solved!Actually, my code flow is wrong.

 $tokenResult = $user->createToken('Personal Access Token');
  if ($request->remember_me) {
         Passport::personalAccessTokensExpireIn(now()->addHours(3));
       } else {
         Passport::personalAccessTokensExpireIn(now()->addMinutes(15));
       } //I tried to add time after create token and that make didn't work.
----------------------------------------------------------------------------------------------------
   if ($request->remember_me) {
            Passport::personalAccessTokensExpireIn(now()->addHours(3));
        } else {
            Passport::personalAccessTokensExpireIn(now()->addMinutes(15));
        }
        $tokenResult = $user->createToken('Personal Access Token');         

Please or to participate in this conversation.