I’m encountering an issue where a POST request made with Axios in my frontend application using React.Js isn’t delivering the refreshToken to my backend API (Node.js and Express). I’m using the MERN stack.
Frontend code:
const refresh = () => {
axios
.post("/v1/users/refresh-token", { refreshToken })
.then((response) => {
setAuth((prev) => {
console.log(JSON.stringify(prev));
console.log(response.data.data.accessToken);
return { ...prev, accessToken: response.data.data.accessToken };
});
return response.data.data.accessToken;
})
.catch((error) => {
console.error(error);
});
};
return refresh;
};
Backend Code: not complete but the relevent part of the code
const refreshAccessToken = asyncHandler(async(req,res)=>{
const incomigRefreshToken = req.cookies.refreshToken || req.body.refreshToken;
console.log(req.body)
console.log(incomigRefreshToken);
if(!incomigRefreshToken)
throw new ApiError(401,"Unauthorized Access");
I tried console logging;
the refresh Token is there on frontend but not there in backend its just empty object {} in req.body.
I tried using fetch instead of axios same issue as above.
2
Answers
It ma be possible that the
req.body
returns promise based answer.Instead of
req.body
try usingreq.json()
Your new code will looks like
your code is totaly well. But error is still. So you Will do double check on this points. >>>
2.Verify that the refreshToken is correctly extracted from wherever it’s stored (e.g., localStorage, state).
3.Check if there are any interceptors or middleware in your Axios configuration that might be modifying the request or the data being sent.
4.Double-check the network tab in your browser’s developer tools to see the actual request being sent and ensure that the refreshToken is included in the request payload.
5.Ensure that the server-side route /v1/users/refresh-token is correctly configured to handle POST requests and extract data from the request body.