Search code examples
laravelapitokenlaravel-passport

How to replace a new api access token with the old one laravel passport


I need to generate an access token while a method on a controller will be get called by a specific route. My question is how to generate a new access token for logged in user and replace it with the old one (in oauth_access_tokens table e.a. updating user status or token).
I need to delete the old access token and replace it by a new one . In my case, The passport has a life-time and every time that user does something or accomplish or fulfill an action (e.g. call a route, etc...), I need to generate a new access token and replace it to the existing one.


Solution

  • In documentation, it is pretty clearly mentioned, you need to use your access token, to generate a refresh token and you can replace your existing access token with that.

    $http = new GuzzleHttp\Client;
    
    $response = $http->post('http://your-app.com/oauth/token', [
        'form_params' => [
            'grant_type' => 'refresh_token',
            'refresh_token' => 'the-refresh-token',
            'client_id' => 'client-id',
            'client_secret' => 'client-secret',
            'scope' => '',
        ],
    ]);
    
    return json_decode((string) $response->getBody(), true);
    

    now there are a number of options you could employ:

    • Revoke when the refresh token has been used and a new access token has been issued
    • Revoke manually by the user associated with the token
    • Revoke after a pre-determined amount of time

    Look at oauth_access_tokens and oauth_refresh_tokens, tables, that will help you to understand what you need to update here manually.