Home > database >  Laravel sanctum $request->validate not return json when validation fails
Laravel sanctum $request->validate not return json when validation fails

Time:09-23

This is the first time i use Sanctum/Jetstream it and i can't understand why, during an incorrect validation $request->validate the message about validation errors in the JSON format does not come to response, but simply shows the main page. Debug mode is enabled, there are no errors in the laravel logs too. I use this example code from the official documentation, but the validation does not response JSON errors, but if the credentials I am sending are correct, i get the correct token.

Route::post('/sanctum/token', function (Request $request) {

    $request->validate([
        'email' => 'required|email',
        'password' => 'required',
        'device_name' => 'required',
    ]);


    $user = User::where('email', $request->email)->first();



    if (! $user || ! Hash::check($request->password, $user->password)) {
        throw ValidationException::withMessages([
            'email' => ['The provided credentials are incorrect.'],
        ]);
    }

    return $user->createToken($request->device_name)->plainTextToken;
});

CodePudding user response:

Did you add Accept: application/json in the header?

  • Related