I am having trouble making a POST request of user info on a register form to the Sanity CMS.I am using Next.js & next-connect. I am getting a 500 error when making the request.
Here is a snippet of code from the register.js front end page:
const submitHandler = async ({
firstName,
lastName,
email,
password,
confirmPassword,
}) => {
if (password !== confirmPassword) {
enqueueSnackbar("Passwords don't match", { variant: "error" });
return;
}
try {
const { data } = await axios.post(`/api/users/register`, {
firstName,
lastName,
email,
password,
});
console.log("hit success")
dispatch({ type: "USER_LOGIN", payload: data });
jsCookie.set("userInfo", JSON.stringify(data));
router.push("/");
} catch (err) {
console.log(err.message)
enqueueSnackbar("Something went wrong, please try again.", { variant: "error" });
}
};
Here is the code from the api/users/router.js file:
handler.post(async (req, res) => {
console.log("hit handler")
const projectId = client.projectId;
const dataset = client.dataset;
const apiVersion = client.apiVersion;
const tokenWithWriteAccess = process.env.SANITY_AUTH_TOKEN;
const createMutations = [
{
create: {
_type: "user",
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: bcrypt.hashSync(req.body.password),
isAdmin: false,
isWholesale: false,
},
},
];
const { data } = await axios.post(
`https://${projectId}.api.sanity.io/v${apiVersion}/data/mutate/${dataset}`,
{
mutations: createMutations,
},
{
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${tokenWithWriteAccess}`,
},
}
);
const userId = data.results[0].id;
const user = {
_id: userId,
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
isWholesale: false,
isAdmin: false,
};
const token = signToken(user);
res.send({ ...user, token });
});
- I've done my best to double check the format of the GROQ requests.
- I made sure that I have localhost allowed through CORS.
- I have double checked that I am using a current access token and all other variables such as projectID, etc. are correct.
- I've verified that the my form handlers are working correctly and that the object I am sending matches the schema I set up in Sanity.io.
- I have used Postman to make a successful GET request to the api/users/register.js end point.
Thanks in advance for any assistance!