Laravel Passport token lifetime

前端 未结 10 1346
轮回少年
轮回少年 2021-01-04 01:12

I don\'t get what I\'m doing wrong. I can\'t set token expiration time.



        
相关标签:
10条回答
  • 2021-01-04 01:46

    Yes, I just wasted one day to find this problem in VERSION = '5.8'.

    For now, maybe we need modify your-project/vendor/laravel/passport/src/Passport.php.

    Change this -----> new DateInterval('P1Y') . it is php function Represents a date interval.

    D---> means Day Y---> means year M---> means Month

    three types of token in passport

    1.tokensExpireIn in 303 line.

    1. personalAccessTokensExpireIn in 341 line .

    2. refreshTokensExpireIn in 322 line.

    0 讨论(0)
  • 2021-01-04 01:47

    you can do this:

    $tokenResult = $user->createToken('Personal Access Token');
    $token = $tokenResult->token;
    $token->expires_at =
            Carbon::now()->addDays(env('PERSONAL_ACCESS_TOKEN_EXPIRY__DAYS'));
    
    $token->save();
    
    0 讨论(0)
  • 2021-01-04 01:48

    File: AuthServiceProvider.php

    Add these lines

    use Laravel\Passport\Bridge\PersonalAccessGrant;
    use League\OAuth2\Server\AuthorizationServer;
    

    Add the following code in boot function

    public function boot() {
         Passport::routes();
         $lifetime = new \DateInterval('PT24H'); // For 24hours
    
         $this->app->get(AuthorizationServer::class)->enableGrantType(new PersonalAccessGrant(), $lifetime);
    }
    
    0 讨论(0)
  • 2021-01-04 01:56

    Here are the methods used to update expiration time for all the grant types :

    Personal access token:

    public function boot(){
            $this->registerPolicies();
    
            Passport::routes();
            Passport::personalAccessTokensExpireIn(Carbon::now()->addHours(24));
            Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
    }
    

    Rest all

    public function boot(){
            $this->registerPolicies();
    
            Passport::routes();
            Passport::tokensExpireIn(Carbon::now()->addHours(24));
            Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
    }
    

    Just update the above code in the boot method of AuthServiceProvider.

    0 讨论(0)
提交回复
热议问题