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

near2587's avatar

post request by axios to laravel giving 500 error

Hey guys, I need help to solve a problem, I have an application with react + laravel that works fine on localhost, but when I publish it on the server, I get this error

POST https ://api.oussama-dev.online/api/login 500 (Internal Server Error)

Te {message: 'Request failed with status code 500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', config: {…}, request: XMLHttpRequest, …}

login.jsx:

const onSubmit=(ev)=>{
  ev.preventDefault();
  setError('')
    email,
    password,
  }).then(({data})=>{
   setCurrentUser(data.user)

   setUserToken(data.token)
   setUserType(data.user_type)
   localStorage.setItem('user_id', data.user.id);
   }

  })
  .catch((error)=>{
    if (error.response) {
      setError(error.response.data.error);
    }
    console.error(error);
  });
};  

Lavael Route is: Route::post('/login',[AuthController::class,'login']);

And Controller is:

public function login(LoginRequest $request)
    {
        $credentials = $request->validated();
        $remember = $credentials['remember'] ?? false;
        unset($credentials['remember']);

        if (!Auth::attempt($credentials, $remember)) {
            return response([
                'error' => 'mot de passe incorrect'
            ], 422);
        }
        $user = Auth::user();
        $token = $user->createToken('main')->plainTextToken;

        return response([
            'user' => $user,
            'token' => $token,
            'user_type' => $user->usertype,

        ]);
    }
0 likes
1 reply
vincent15000's avatar

Here are some question which can perhaps help you find the reason.

Have you the react frontend on the domain than the Laravel backend ?

Have you checked that the PHP version is exactly the same as in local ?

Is it normal that you have twice api in your post route ? https ://API.oussama-dev.online/API/login

You have a space inside the URL between the s and :.

Please or to participate in this conversation.