I am having an issue where I am not able to grab my cookies from my node.js backend. Below is my current code and when I console.log(req.cookies) It returns [Object: null prototype] {}. I had a similar issue in the past and the way I fixed that was just by adding "withCredentials: true" but since I already have that on my axios post request I don't believe that to be the issue.
Front-end
const save = async () => {
if (loggedInCookie) {
await axios.post('http://localhost:5000/savelisting', {
withCredentials: true,
link: link,
car: car,
price: price,
picture: picture,
timeleft: timeleft,
site: site,
milage: milage,
location: location,
trans: trans
});
} else {
console.log("please login to save this listing")
};
};
Backend
app.post('/savelisting', async (req, res) => {
console.log(req.cookies);
try {
var jwtToken = await req.cookies.AccessToken;
console.log(jwtToken);
// Grooms cookie
jwtToken = jwtToken
.split('; ')
.find(row => row.startsWith('AccessToken='))
.split('=')[1];
const decoded = jwt.verify(jwtToken, process.env.TOKEN_KEY);
var userId = decoded.id
console.log(userId)
const link = req.body.link;
const car = req.body.car;
const price = req.body.price;
const picture = req.body.picture;
const timeleft = req.body.timeleft;
const site = req.body.site;
const milage = req.body.milage;
const location = req.body.location;
const trans = req.body.trans;
} catch(err) {
console.log(err)
};
res.status(200).send();
});
CodePudding user response:
You are sending the withCredentials
setting as part of the data to your API. It should be passed as an option to axios:
axios.post(
'http://localhost:5000/savelisting',
{
link: link,
car: car,
...
},
{
withCredentials: true
}
)