laravel - 使用 Passport 和 Laravel 进行身份验证时返回访问 token 和用户

标签 laravel api laravel-passport

我正在使用 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;

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

最佳答案

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

关于laravel - 使用 Passport 和 Laravel 进行身份验证时返回访问 token 和用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43412235/

相关文章:

php - Laravel 身份验证自定义规则

laravel - 无法重新启动 supervisor.service : Unit not found CentOS 7

javap 以可编程的方式

ruby - 如何使用 Ruby 在 Hadoop HDFS 中写入和读取文件?

使用 Passport 和 React 进行 Laravel API 身份验证

php - 我在 cPanel 中上传了一个 laravel 项目,但收到此错误 : SQLSTATE[HY000] [1044] Access denied for user

c - OpenSSL C API CRL 检查

laravel - 混淆 Laravel 护照 API 安全性的工作原理

php - Laravel 护照 : registered date claims is deprecated

mysql - 在 Laravel Eloquent 中使用桥接表中的关系