I’m honestly kind of new to react, and when trying to submit a signup form after filling out the details, I keep getting the error “Unexpected end of JSON input”, and on my node terminal it shows this:
11:58:50 PM [vite] http proxy error: /api/auth/signup
AggregateError [ECONNREFUSED]:
at internalConnectMultiple (node:net:1116:18)
at afterConnectMultiple (node:net:1683:7)
this is the code block:
import { useState } from "react";
import toast from "react-hot-toast";
import { useAuthContext } from "../context/AuthContext";
const useSignup = () => {
const [loading, setLoading] = useState(false);
const { setAuthUser } = useAuthContext();
const signup = async ({ fullName, username, password, confirmPassword, gender }) => {
const success = handleInputErrors({ fullName, username, password, confirmPassword, gender });
if (!success) return;
setLoading(true);
try {
const res = await fetch("/api/auth/signup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ fullName, username, password, confirmPassword, gender }),
});
const data = await res.json();
if (data.error) {
throw new Error(data.error);
}
localStorage.setItem("chat-user", JSON.stringify(data));
setAuthUser(data);
} catch (error) {
toast.error(error.message);
} finally {
setLoading(false);
}
};
return { loading, signup };
};
export default useSignup;
function handleInputErrors({ fullName, username, password, confirmPassword, gender }) {
if (!fullName || !username || !password || !confirmPassword || !gender) {
toast.error("Please fill in all fields");
return false;
}
if (password !== confirmPassword) {
toast.error("Passwords do not match");
return false;
}
if (password.length < 6) {
toast.error("Password must be at least 6 characters");
return false;
}
return true;
}
hoping that this can be answered soon, thanks!