Answer the question
In order to leave comments, you need to log in
Why can't I log in through the test?
I'm doing an http test, but I'm getting
Expected status code 200 but received 500.
Failed asserting that false is true.
public function testLogin()
{
$responca = $this->post('/api/login', [
'username' => 'r.absalyamov',
'password' => 'kazan1811'
]);
$responca->assertStatus(200);
}
public function login(Request $request)
{
// Проверяем на пустые поля
$errors = Validator::make($request->all(), [
'username' => 'required',
'password' => 'required'
])->errors();
Если есть значение то выводим 401 ошибку
if (count($errors)){
return response(['errors' => $errors], 401);
}
//Проверяем есть ли пользователь
$user = User::where('name', $request->username)->first();
if (!$user)
return response(['status' => 'error', 'message' => 'Пользователь не найден'], 401);//Если пользователя нет, то выводим 401 ошибку
//Проверяем пароль
if (Hash::check($request->password, $user->password)){
//Если все нормально, то создаем api токен
$response = new ApiTokenUser();
$response = $response->setToken($user->email, $request->password);
$response = json_decode((string) $response->getBody(), true); // И формируем ответ
} else {
$response = ['errors' => true, 'message' => 'Неверный пароль']; // формируем ответ
}
//Выводим клиенту что пришло
return response($response);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question