'Deleting cookie in client from server failing (Firefox)
Im trying to delete the cookies in the client that my server created every time the user logout, they have httpOnly:true attr so only the server response can delete it automatically. I already tried my app in Chrome, Firefox, Edge. Its working will with the other 2 browser, the code deletes it automatically from the cookies after logout, but in firefox its not, the cookies is still there. Here's my code
server/backend
const logoutUser = asyncHandler(async(req, res) => {
const cookies = req.cookies;
res.cookie('jwt', 'none', {
expires: new Date(Date.now() + 1 * 1000),
httpOnly: true,
secure: true,
sameSite: 'None',
})
res.clearCookie('jwt', { httpOnly: true, sameSite: 'None', secure: true });
res.sendStatus(204);
if (!cookies?.jwt) return res.sendStatus(401); //No content
const refreshToken = cookies.jwt;
// Is refreshToken in db?
const foundUser = await User.findOne({ refreshToken }).exec();
if (!foundUser) {
res.clearCookie('jwt', { httpOnly: true, sameSite: 'None', secure: true });
return res.sendStatus(204);
}
// Delete refreshToken in db
foundUser.refreshToken = foundUser.refreshToken.filter(rt => rt !== refreshToken);
const result = await foundUser.save();
console.log(result);
})
client/frontend
const logout = async () => {
localStorage.removeItem('userpersist')
document.cookie = "userpersist=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
try {
await apiClient.get('/logout', {
withCredentials: true
});
} catch (err) {
console.error(err);
}
}
And also another question: Why I'm instantly redirected to home page '/' even though I haven't set up my navigation yet after user press logout?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
