0

I'm trying to set-up a route that delete cookie containing a JWT on the client's browser.

To perform that, I'm using the res.ClearCookie function

    public async logOut (req: Request, res: Response) {

        res.clearCookie('auth-token', {httpOnly: true, path:'/', domain: 'localhost'});

        console.log('cookie deleted')

    }

I have seen that clearCookie function has to contain the same object that I passed during it creation, so here is the way I created it

const accessToken: string = jwt.sign({id: existingUser.id}, process.env.ACCESS_TOKEN_SECRET || 'tokensecret' )

return res.cookie('auth-token', accessToken, {httpOnly: true, path:'/', domain: 'localhost'}).json(mainWallet[0].id)

This way, the cookie isn't deleted when I try to logout.

Do you have an idea to fix that ?

Thanks,

Paul

x2cheese
  • 273
  • 5
  • 17
  • Maybe this https://stackoverflow.com/questions/27978868/destroy-cookie-nodejs#comment115315388_27982797 helps you – Anatoly Dec 20 '20 at 17:25
  • hey @Anatoly, thanks you for your response. Sadly, none of the solutions suggested in the other thread work in my case... – x2cheese Dec 21 '20 at 08:04

2 Answers2

0

I have followed a new tutorial that say to setup the log-out route this way. this time it's works

res.status(202).clearCookie('auth-token').send('cookie cleared')
x2cheese
  • 273
  • 5
  • 17
0

use these set of codes to construct your Api. this is the cleanest and shortest i have found for logout

export const logout = async (req, res, next) => {
  res.clearCookie("jwt");
  res.redirect("/");
};

This only clear the tokens stored in the cookie