Laravel passport refresh token

三世轮回 提交于 2020-01-01 12:20:08

问题


I am using a Laravel version 5.5 using Passport for authentication. I have successfully create the token and can access it using the auth:api middleware.

But whenever user login into system it create new token for that user. I just want to refresh user last token and send it back instead of creating a new token.

I have used the following code to generate auth token

$token = $user->createToken('string-'.$user->id)->accessToken;

It generate the token with 1075 characters but when i checked in database table oauth_access_tokens it shows me the token with 80 characters.

How can i get last generated token using 80 character token and refresh it and send it back?

Thanks in Advance


回答1:


If your application issues short-lived access tokens, users will need to refresh their access tokens via the refresh token that was provided to them when the access token was issued. In this example, we'll use the Guzzle HTTP library to refresh the token:

$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);

This /oauth/token route will return a JSON response containing access_token, refresh_token, and expires_in attributes. The expires_in attribute contains the number of seconds until the access token expires.



来源:https://stackoverflow.com/questions/53774731/laravel-passport-refresh-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!