【问题标题】:Return the access token and the user when authenticating using Passport and Laravel使用 Passport 和 Laravel 进行身份验证时返回访问令牌和用户
【发布时间】:2017-09-10 18:06:46
【问题描述】:

我正在使用 Laravel 5.4 和 Passport 创建一个 API。 API 授权使用密码授权类型完成。

下面是一个示例请求:

$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
  'form_params' => [
     'grant_type' => 'password',
     'client_id' => 'client-id',
     'client_secret' => 'client-secret',
     'username' => 'taylor@laravel.com',
     'password' => 'my-password',
     'scope' => '',
   ],
]);

return json_decode((string) $response->getBody(), true);

这将向“/oauth/token”发送一个 POST 请求,响应如下:

{
  "token_type": "Bearer",
  "expires_in": 3155673600,
  "access_token": "eyJ0eXAiOiJK...",
  "refresh_token": "LbxXGlD2s..."
}

我想要的是得到一个响应,包括经过身份验证的用户,如下所示:

[
  data:{
     name: Jhon,
     email: jhon@example.com,
     address: ASDF Street no.23
  },
  token{
     "token_type": "Bearer",
     "expires_in": 3155673600,
     "access_token": "eyJ0eXAiOiJK...",
     "refresh_token": "LbxXGlD2s..."
  }
]

我已经在第 65 行更改了 PasswordGrant 文件

vendor/league/oauth2-server/src/Grant/PasswordGrant.php

$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);
return $responseType;

我希望有人可以帮助我,并告诉我如何解决这个问题, 谢谢。

【问题讨论】:

    标签: laravel api laravel-passport


    【解决方案1】:

    您可以编写一个 (after-) middleware 来获取原始响应并将其转换为您想要的方式。但请注意,所有其他 Passport 中间件(CheckClientCredentialsCheckScopesCreateFreshApiToken 等)可能都取决于该特定格式,并且也必须进行调整。

    【讨论】:

      猜你喜欢
      • 2019-12-20
      • 2018-04-18
      • 2019-11-29
      • 1970-01-01
      • 2014-02-11
      • 2023-03-20
      • 1970-01-01
      • 2017-07-08
      • 2019-01-31
      相关资源
      最近更新 更多