In my web application I have a button for deleting user account , when clicked, the user account coudn't be deleted even though I get a 200 status code in the frontend
React
const DeleteAccount = async () => {
setShow(false);
setOpenModalLoader(true);
const id = appAuthState.user._id;
console.log(" id delete account =>", appAuthState.user._id); // logged properly
try {
const res = await axios.delete(
"/user/delete",
{ data: { id } },
{ withCredentials: true }
);
console.log("deleted user", res.status, res.data); // res.status => 200 , res.data => empty
deleteNotify(res.status);
setOpenModalLoader(false);
if (res.status === 200) {
navigate("/");
}
} catch (e) {
console.log("error from delete user", e);
setOpenModalLoader(false);
deleteNotify();
}
};
express
routerUser.delete("/user/delete", isLoggedIn, async (req, res) => {
console.log("request body => ", req.body); // not logged in cmd
try {
let deletedUser = await User.findByIdAndDelete(req.body.id);
//console.log("from user delete route", deletedUser); // not logged
res.status(200).send(deletedUser);
} catch (e) {
res.send(e);
}
});
I'm not getting any errors but console.log inside delete route doesn't log anything in cmd not even "request body => "
Notes:
*) Other routerUser routes work properly with isLoggedIn middleware
*) For a specific reason I don't want to use req.user ,I think req.body should work too
*) If this could help : all other axios calls that work as expected are from localhost:3000/ the deletion call is the only one from localhost:3000/accountdeletion and I'm using cors with the correct configuration (I don't think this is important but who knows)
what I did wrong?
(sorry,english isn't my native language)