0

Im trying to add a logout functionality to my mern app where I only have google authentication using passportJS. I want to clear the cache and cookies on logout because if I don't it automatically logs me back in without asking for what account to choose.

The cookie im trying to delete is a https only cookie

I have tried what seems like every solution on the internet but none of them work

logout

    document.cookie = "connect.sid=; Max-Age=0;secure;path=/"; // This worked only for the first time
// clears cache which works fine
    caches.keys().then((names) => {
      names.forEach((name) => {
        caches.delete(name);
      });
    });
    window.open(`${apiURL}/auth/logout`, "_self");

I have tried other solutions like: react-cookie, universal-cookie, js-cookie etc... But none of them seem to work either.

Does anyone know how to do this I have been stuck on this for a long time?

  • Are you sure you're clearing cookies for the right domain/subdomain and path? Check the browser's inspector for the exact details of the cookies. See https://stackoverflow.com/questions/595228/how-can-i-delete-all-cookies-with-javascript for more details/exploration. – ceejayoz Nov 12 '22 at 03:36
  • Also the cookie im trying to delete is a https only cookie –  Nov 12 '22 at 03:49

1 Answers1

0

Ok so after a while I figured out that I cannot delete http only cookies so what I did was I simply used the req.logout() function on the backend to delete it

when you log the user out you simply redirect the user to this route which then deletes the cookie and redirects back!

router.get("/logout", (req: Request, res: Response) => {
  req.logout({}, (err: any) => {
    if (err) return res.status(500).json({ message: "Something went wrong." });
    res.redirect(clientURL);
  });
});