Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 11, 2022 07:15 pm GMT

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 :-

Image description

And it seems like
res.clearCookie("jwt")
is not working here

Image description

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To