1

I receive this error when I try to login with wrong credentials :

<!doctype html>
<html class="theme-light">
<!--
TypeError: Argument 2 passed to Symfony\Component\HttpFoundation\JsonResponse::__construct() must be of the type integer, array given, called in /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php on line 31 in file /home/stylmyvz/cars.styleshopeg.com/vendor/symfony/http-foundation/JsonResponse.php on line 42

#0 /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php(31): Symfony\Component\HttpFoundation\JsonResponse->__construct(Array, Array, Array)
#1 /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php(99): Illuminate\Http\JsonResponse->__construct(Array, Array, Array, 0)
#2 /home/stylmyvz/cars.styleshopeg.com/app/Http/Controllers/APILoginController.php(31): Illuminate\Routing\ResponseFactory->json(Array, Array)
#3 [internal function]: App\Http\Controllers\APILoginController->login(Object(Illuminate\Http\Request)
I want to receive JSON response like this in Postman : 
{
  "error":"invalid username or password"
}

Here is Login Controller and I instruct it to give me "invalid username or password"

Can anyone tell me what is wrong with it

class APILoginController extends Controller
{
    //
    public function login(Request $request){
        $validator = Validator::make($request -> all(),[
            'email' => 'required|string|email|max:255',
            'password' =>'required'
        ]);

        if($validator->fails()){
            return response()->json($validator->errors());
        }

        $credentials = $request->only('email', 'password');
        try {
            if(! $token = JWTAuth::attempt($credentials))
            {
                return response()->json(['error'=>'invalid username or password'], [401]);
            }
        } catch (JWTException $e) {
            //throw $th;
            return response()->json(['error'=>'could not create token'], [500]);
        }

        return response()->json(compact('token'));

    }
}

Contents of api.php file:

Route::post('user/register','APIRegisterController@register');

Route::post('user/login','APILoginController@login');

Route::middleware('jwt.auth')->get('/users', function (Request $request) {
    return auth()->user();
});

Route::middleware('jwt.auth')->group( function(){
   Route::resource('/cars', 'API\CarController');
} );
5
  • 3
    You need to send a POST request from Postman Commented May 28, 2020 at 20:33
  • It would be helpfull if you would post the contents of your route.php or api.php file Commented May 28, 2020 at 20:38
  • @SalimDjerbouh I do send a POST request and that is what I receive when I send it Commented May 28, 2020 at 20:57
  • @jobvink I added api.php file above but I do not know where to find route.php file Commented May 28, 2020 at 20:58
  • @SalimDjerbouh I replaced the error I get with POST request Commented May 28, 2020 at 21:06

2 Answers 2

1

I found the problem it was in the error message number I passed it as an array

return response()->json(['error'=>'invalid username or password'], [401]);

and I should remove the brackets and pass it like int

return response()->json(['error'=>'invalid username or password'], 401);
Sign up to request clarification or add additional context in comments.

Comments

1

Please Change this try & catch both..

try {
            if(! $token = JWTAuth::attempt($credentials))
            {
                return response()->json(['error'=>'invalid username or password'], 401);
            }
        } catch (JWTException $e) {
            //throw $th;
            return response()->json(['error'=>'could not create token'], 500);
        }

This is how I do it in Laravel....

return Response::json(['message' => $value,'other_info'=>$vlue2],201);

Or using a helper function:

return response()->json(['message' => $value,'other_info'=>$vlue2], 201); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.