An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
April 11, 2022 07:15 pm GMT
Original Link: https://dev.to/paradox098/need-help-unable-to-delete-cookie-in-express-4o93
Need Help unable to delete cookie in Express
Hello everyone I am a newbie working on a full stack web app using node, express and mongodb in the backend for my final semester project.
I am working on an admin portal, when users (admins) sign in a cookie is stored in the browser and a token (using json web tokens here) is stored in the mongodb, but I am unable to delete the cookie during logout.
My logout code is :-
app.get("/logout", auth, async (req, res) => { try { req.user.tokens = req.user.tokens.filter((currentElement) => { return currentElement.token !== req.token }) res.clearCookie("jwt"); console.log("Logout Successful"); await req.user.save(); res.render("/"); } catch (error) { res.status(500).send(error); }});
Authorization code :-
const auth = async (req, res, next) => { try { const token = req.cookies.jwt; const verifyUser = jwt.verify(token, process.env.SECRET_KEY); console.log(verifyUser); const user = Register.findOne({ _id :verifyUser._id}); console.log(user.firstname); req.token = token; req.user = user; next(); } catch (error) { res.status(401).send(error); }}
Logout only gives this output :-
And it seems like
res.clearCookie("jwt")
is not working here
as the jwt cookie is not getting deleted Please help I am stuck from weeks. Unable to figure out how to make it work.
Original Link: https://dev.to/paradox098/need-help-unable-to-delete-cookie-in-express-4o93
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To