我正在尝试使用枪口制作一个身份验证系统,但事实证明它抛出此错误:
Client error: `POST http://example.com/api/login` resulted in a `401 Unauthorized` response: {"error":"invalid_credentials"}
我已经在Postman中使用正确的键和值检查了API网址,并且得到了正确的响应:
{
"success": true,
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEwMCwiaXNzIjoiaHR0cDovL2Rldi50b2tvaGFqaS5jby5pZC9hcGkvbG9naqwegzxvhyXQiOjE1OTE1NDU3NTUsImV4cCIasdqwegfxdzc6MTU5Mjc1NTM1NSwibmJmIjoxNTkxNTQ1NzU1LCJqdGkiOiJ2QmpZVHhaYXBYNjNvb21XIn0.RAKbWMSjk6I20LxczS9TFEUKV3f7t_fg84q89nhavN8"
}
这是我的控制器:
public function postLogin(Request $request)
{
request()->validate([
'login' => 'required',
'password' => 'required',
]);
$client = new Client;
$response = $client->post('http://example.com/api/login', [
'form-data' => [
'login' => $request->login,
'password' => $request->password
]
]);
if (Auth::attempt($response)) {
if (!empty (Auth::users())) {
$events = $response['success'];
foreach($events as $item)
{
DB::table('users')->insert(['phone'=>$item['phone'], 'password'=>$item['password']]);
return redirect()->intended('/');
}
} else {
// Authentication passed...
return redirect()->intended('/');
}
}
return Redirect::to('ecommerce.login')->withSuccess('Invalid credentials');
}
说明:在这里,我尝试将值发布到API url,如果成功,并且数据库中没有关于它的数据,那么用户登录数据(电话号码和密码)将保存到应用程序数据库中,并且用户将登录,否则=用户将立即转到主页。如果失败,用户将返回到登录页面。
这是我的路线:
Route::get('/login', 'Ecommerce\FrontController@login')->name('front.login');
Route::post('post-login', [ 'as' => 'post-login', 'uses' => 'Ecommerce\LoginController@postLogin']);
这是我的刀刃:
<form action="{{ route('post-login') }}" method="post">
@csrf
<div class="container">
<div class="text-center" style="color:#00843D;">
<p style="padding-top:20px;">Welcome</p>
</div>
<hr>
<div class="form-group">
<input class="form-control {{ $errors->has('login') ? ' is-invalid' : '' }}" type="text" name="login" placeholder="Phone Number" value="{{ old('login') }}" autofocus required>
</div>
<div class="form-group">
<input class="form-control {{ $errors->has('login') ? ' is-invalid' : '' }}" type="password" name="password" placeholder="Password" required>
</div>
<div class="form-group">
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<button class="registerbtn" type="submit">Login</button>
</div>
</div>
<div class="container signin" style="padding-bottom:30px;">
<p>Doesn't have account? <a href="{{ route('front.register') }}">Register in here</a>.</p>
</div>
</form>
我已经尝试解决了很多时间,但仍然找不到答案。希望您能帮助我,谢谢。
答案 0 :(得分:0)
您需要先获得身体。
之后:
$response = $client->post('http://example.com/api/login', [
'form-data' => [
'login' => $request->login,
'password' => $request->password
]
]);
$json = json_decode((string) $response->getBody());
然后,您就可以使用响应中的json。
答案 1 :(得分:0)
我解决了!
因此,我尝试查找有关API完整网址时的外观的一些参考,它将使我找到答案!
在这里,我将控制器更改为此:
$login = $request->login;
$password = $request->password;
$client = new Client;
$response = $client->post('http://example.com/api/login?login='.$login.'&password='.$password);
$json = json_decode((string) $response->getBody(), true);
感谢@JoeGalind帮助我! :)