1

I've been researching for all internet why do I get this error when I try to POST on the login api route:

Status 419 unknown status Version HTTP/1.1 Transferred 6.94 KB (6.46 KB size) Referrer Policy strict-origin-when-cross-origin 

This is what I have on my UserController.php:

 public function userSignUp(Request $request) { $validator = Validator::make($request->all(), [ "name" => "required", "email" => "required|email", "password" => "required", ]); if($validator->fails()) { return response()->json(["status" => "failed", "message" => "validation_error", "errors" => $validator->errors()]); } $userDataArray = array( "name" => $request->name, "email" => $request->email, "password" => md5($request->password), ); $user_status = User::where("email", $request->email)->first(); if(!is_null($user_status)) { return response()->json(["status" => "failed", "success" => false, "message" => "Ooops! Email ya registrado anteriormente"]); } $user = User::create($userDataArray); if(!is_null($user)) { return response()->json(["status" => $this->status_code, "success" => true, "message" => "Registro completado correctamente", "data" => $user]); } else { return response()->json(["status" => "failed", "success" => false, "message" => "Fallo al registrar"]); } } // ------------ [ User Login ] ------------------- public function userLogin(Request $request) { $attr = $request->validate([ 'email' => 'required|string|email|', 'password' => 'required|string|min:6' ]); if (!Auth::attempt($attr)) { return response()->json([ 'message' => 'Invalid login details' ], 401); } $token = auth()->user()->createToken('auth_token')->plainTextToken; $user = auth()->user(); $respon = [ 'status' => 'success', 'msg' => 'Login successfully', 'content' => [ 'status_code' => 200, 'access_token' => $token, 'token_type' => 'Bearer', 'user_name' => $user->name, 'user_email' => $user->email, 'user_id' => $user->id, ] ]; return response()->json($respon, 200); } 

And I have these routes on api.php:

 Route::post("login", [UserController::class, "userLogin"]); Route::post("register", [UserController::class, "userSignUp"]); 

These routes works perfectly on the RESTED add-on on Firefox. This is on api.php, I shouldn't have any problem with csrf token.

5
  • I've been researching for all internet a little bit more. Do you use Laravel? Does this answer your question? Laravel 5.5 ajax call 419 (unknown status). It might be the same due because of strict-origin-when-cross-origin in your error. Commented May 16, 2022 at 15:42
  • I do use laravel. That doens't work, as csrf token isn't neccesary for api.php requests. CORS can't be the problem, as I'm working on localhost, being the laravel server port 8000 and the react app on 3000. All GET requests works perfectly, only POST are affected. Commented May 16, 2022 at 15:50
  • can you show the address that you have specified in the action attribute on the form with the post request? Commented May 16, 2022 at 17:04
  • 419 error is because the system has generated a new csrf token when the session was established. You say this should not be necessary on API calls. This would be true if it was stateless, but you are using Auth::attempt ? Commented May 16, 2022 at 17:20
  • Does this answer your question? Why does the Laravel API return a 419 status code on POST and PUT methods? Commented May 16, 2022 at 17:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.