使用 Passport 和 Laravel 进行身份验证时返回访问令牌和用户 [英] Return the access token and the user when authenticating using Passport and Laravel
问题描述
我正在使用 Laravel 5.4 和 Passport 创建 API.API 授权是使用密码授权类型完成的.
以下是请求示例:
$http = new GuzzleHttp\Client;$response = $http->post('http://your-app.com/oauth/token', ['form_params' =>['grant_type' =>'密码','client_id' =>'客户ID','client_secret' =>'客户机密','用户名' =>'taylor@laravel.com','密码' =>'我的密码','范围' =>'',],]);返回 json_decode((string) $response->getBody(), true);
这将向/oauth/token"发送一个 POST 请求,响应如下:
<代码>{"token_type": "承载",expires_in":3155673600,"access_token": "eyJ0eXAiOiJK...","refresh_token": "LbxXGlD2s..."}
我想要的是得到一个包含经过身份验证的用户的响应,如下所示:
<预><代码>[数据:{姓名:约翰,电子邮件:jhon@example.com,地址:ASDF Street no.23},令牌{"token_type": "承载",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);返回 $responseType;
我希望有人可以帮助我,并告诉我如何解决这个问题,谢谢.
你可以写一个 (after-) 中间件 获取原始响应并将其转换为您希望的方式.但请注意,所有其他 Passport 中间件(CheckClientCredentials
、CheckScopes
、CreateFreshApiToken
等...)可能取决于该特定格式,并且将具有也要适应.
I am creating an API using Laravel 5.4 and Passport. The API authorization is done using Password grant type.
Below is a sample request:
$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);
This will send a POST request to '/oauth/token' with the following response:
{
"token_type": "Bearer",
"expires_in": 3155673600,
"access_token": "eyJ0eXAiOiJK...",
"refresh_token": "LbxXGlD2s..."
}
What I want is to get a response including the authenticated user as shown below:
[
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..."
}
]
What I already did was alter the PasswordGrant file at line 65
vendor/league/oauth2-server/src/Grant/PasswordGrant.php
$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);
return $responseType;
I hope someone can help me, and tell me how to resolve this, thank you.
You could write an (after-) middleware that takes the original response and transforms it to be the way you want it to be. But be aware that all other Passport middleware (CheckClientCredentials
, CheckScopes
, CreateFreshApiToken
, etc...) probably depends on that specific format and will have to be adapted as well.
这篇关于使用 Passport 和 Laravel 进行身份验证时返回访问令牌和用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!