【问题标题】:Is there a way to refresh Laravel Passport's cookie tokens with a Vue.JS SPA?有没有办法用 Vue.JS SPA 刷新 Laravel Passport 的 cookie 令牌?
【发布时间】:2019-08-27 02:19:15
【问题描述】:

对于 SPA 仪表板,我使用 Laravel Passport 通过 CreateFreshApiToken Web 中间件提供 API 令牌。它工作正常,尽管当用户在仪表板中处理内容时,令牌过期后他将无权检索/发布数据,这也是预期的行为。有没有办法通过 API 刷新 cookie 令牌?例如:每次用户处于活动状态时,也就是从 api 发布或检索某些内容时,cookie 令牌将被刷新,因此它只会在用户处于非活动状态时过期。

【问题讨论】:

    标签: javascript php laravel vue.js laravel-passport


    【解决方案1】:

    如果您的应用程序发出短期访问令牌,用户将需要通过在发出访问令牌时提供给他们的刷新令牌来刷新他们的访问令牌。

    Laravel

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

    VUEJS

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

    【讨论】:

    • 刷新令牌请求是否适用于CreateFreshApiToken Web 中间件设置的基于 cookie 的令牌?
    • 使用CreateFreshApiToken 中间件时不会生成刷新令牌。如果我们谈论的是使用 Laravel Passport 提供的 OAuth 2.0 密码授权进行身份验证,那么这个答案是正确的。
    【解决方案2】:

    Passport 的RouteRegistrar 类有a route registered 用于此目的。据我所知,Laravel Passport 的官方文档中奇怪地没有记录它。

    您可以向oauth/token/refresh 端点执行POST 请求,并随时接收新的访问令牌。 这个请求不需要任何有效负载,唯一的要求是您使用 Laravel 中的传统会话登录。

    请注意,由于此路由使用 web 中间件组,因此您的会话也会在请求新的访问令牌时自动延长。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-19
      • 2020-09-14
      • 2020-10-10
      • 2017-10-15
      • 2021-06-15
      • 2015-08-17
      • 1970-01-01
      • 2018-04-12
      相关资源
      最近更新 更多