Skip to content

Commit

Permalink
extract method. unify format
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 19, 2017
1 parent 31f09dc commit 87485e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Illuminate/Foundation/Auth/AuthenticatesUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ protected function authenticated(Request $request, $user)
protected function sendFailedLoginResponse(Request $request)
{
if ($request->expectsJson()) {
return response()->json(['message' => trans('auth.failed')], 422);
return response()->json([
'message' => 'Authentication failed.', 'errors' => [
$this->username() => [trans('auth.failed')],
],
], 422);
}

return redirect()->back()
Expand Down
18 changes: 15 additions & 3 deletions src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,28 @@ protected function failedValidation(Validator $validator)
public function response(array $errors)
{
if ($this->expectsJson()) {
return new JsonResponse([
'message' => 'The given data was invalid.', 'errors' => $errors,
], 422);
return $this->jsonResponse($errors);
}

return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}

/**
* Get the proper failed validation JSON response for the request.
*
* @param array $errors
* @return \Illuminate\Http\JsonResponse
*/
protected function jsonResponse(array $errors)
{
return new JsonResponse([
'message' => 'The given data was invalid.',
'errors' => $errors,
], 422);
}

/**
* Format the errors from the given Validator instance.
*
Expand Down

0 comments on commit 87485e6

Please sign in to comment.