【发布时间】:2020-07-17 14:30:33
【问题描述】:
我无法同时获取用户数据和不记名令牌。使用 vue 2.5 和 laravel 7.2.2
我正在使用 GuzzleHttp 向服务器发出请求以获取令牌。
为了请求通过,我运行了第二个php artisan serve --port 8006 命令。
用户存在于数据库中 - 已验证。
我的 AuthController:
use App\User;
use App\Shop;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use \GuzzleHttp\Client;
use DB;
public function login(Request $request)
{
$user = User::where('email', $request->email)->get();
$client = new Client([
'base_uri' => 'http://127.0.0.1:8006',
'timeout' => 5
]);
try {
$response = $client->request('POST', 'oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => DB::table('oauth_clients')->where('id', 2)->first()->secret,
'username' => $request->email,
'password' => $request->password,
]
]);
$user->token = $response->getBody();
return $user;
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
if ($e->getCode() === 400 || $e->getCode() == 401) {
return response()->json(['message' => 'Грешно потребителко име или парола'], $e->getCode());
}
return response()->json('Something went wrong on the server.', $e->getCode());
}
}
回复是:
[
{
"id": 1,
"name": "Nelly",
"email": "n@n.com",
"email_verified_at": null,
"created_at": "2020-04-05T11:18:30.000000Z",
"updated_at": "2020-04-05T11:18:30.000000Z"
}
]
如果我将方法设为return $response->getBody(),我只会得到不记名令牌。
如果我将方法设为return $user,我只会得到用户
尝试使用 GuzzleHttp\Promise\Promise 如下:
public function login(Request $request)
{
$user = User::where('email', $request->email)->get();
$client = new Client([
'base_uri' => 'http://127.0.0.1:8006',
'timeout' => 5
]);
try {
$promise = $client->requestAsync('POST', 'oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => DB::table('oauth_clients')->where('id', 2)->first()->secret,
'username' => $request->email,
'password' => $request->password,
]
]);
$promise->then(
// $onFulfilled
function ($value) {
dd('The promise was fulfilled.');
},
// $onRejected
function ($reason) {
dd('The promise was rejected.');
}
);
dd('outside of the then');
}
}
每次尝试都返回“当时之外”
【问题讨论】:
标签: php laravel vue.js guzzle laravel-passport