Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LaraBABA's avatar

Return error on failed login - Laravel 5.5

Hello,

I am trying to return my own error as json on failed login via ajax but I have a small problem

  if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
            // Authentication passed...
            return response()->json(['response' => 'success', 'message' => 'You are logged in!']);

        }else{

            return response()->json(['response' => 'failed', 'message' => 'Sorry, you need to be logged in!']);

        }

my second "else" response return is being bypass by Laravel owns json response that outputs:

{"message":"The given data was invalid.","errors":{"email":["These credentials do not match our records."]}}

How can I return anything from a failed Auth::attempt please?

I would like to d() the Auth::attempt but with json I cannot manage(any tricks on this?)

Thanks

0 likes
2 replies
jnobel's avatar

you could change the laravel lang file in /resources/lang/en/auth.php and you should see

'failed' => 'These credentials do not match our records.',
Snapey's avatar

So the issue is not with the code you have shown?

Do you have the auth middleware in place ? It looks like you are hitting that instead and then being passed to the regular auth controller.

Check your routes and middleware

Please or to participate in this conversation.