Categories
Mastering Development

how to clear all cookies when user logout

i have create a cookies when user login that carry the authenticated token of the user now i created function where user can log out from his all account in all devices this was work when i store jwt in localStorage but i remove this to cookies for more security but when i run this functionality it logout from all devices but the cookie that carry the token still there it just remove the cookie from the device i logged out from so is there is any way to remove the cookie from othere devises too

this is my log out all devices function it tried to use clearCookies and change the maxAge of the cookie but this only work for the device i logged out from

exports.signoutAll = async (req, res) => {
  try {
    const user = await User.findById(req.user.id);
    user.tokens = [];
    res.cookie('token', { maxAge: 0 });
    await user.save();
    io.emit('log out all', user);
    res.json(user);
  } catch (err) {
    console.error(err.message);
    res.status(500).json({ msg: 'Error happen in this process' });
  }
};  

Leave a Reply

Your email address will not be published. Required fields are marked *