简体   繁体   中英

Is there a way to refresh Laravel Passport's cookie tokens with a Vue.JS SPA?

For a SPA dashboard I use Laravel Passport to provide API tokens trough the CreateFreshApiToken web middleware. It works fine although when the user is working on stuff in the dashboard he will not have access to retrieve/post data after the token has expired, which aswell is behaviour as expected. Is there a way to refresh the cookie token trough the API? Like for example: Everytime the user is active, aka posts or retrieves something from the api, the cookie token will be refreshed so it will only expire when the user is inactive.

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.

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

created() {
   this.$cookie.set("token", keyValue, "expiring time")
}

There is a route registered by Passport's RouteRegistrar class with this purpose. As far as I can tell it is strangely left undocumented in the official documentation for Laravel Passport.

You can perform a POST request to the oauth/token/refresh endpoint, and receive a new access token at any time. This request requires no payload, the only requirement is that you are logged in, using a traditional session in Laravel.

Note that because this route uses the web middleware group, your session will also automatically be prolonged upon requesting a new access token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM