Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
why my res.clearCookie and req.session.destroy not clearing cookies

I am following bens graphql Reddit tutorial. I have checked my variables and they are coorect here is my graphql logout code:

 @Mutation(() => Boolean)
 logOut(
    @Ctx() { req, res }: MyContext
 ) {
    return new Promise(resolve => req.session.destroy(err => {
        console.log("logout is called")
        res.clearCookie(COOKIE_NAME, { domain: "localhost", path: "/", 
        expires:new Date(Date.now())})
        if (err) {
            console.log("err",err)
            return resolve(false)
        }
        resolve(true)
    }))
}

my clear cookie code:

res.clearCookie(COOKIE_NAME, { domain: "localhost", path: "/", 
httpOnly:true, sameSite:"lax"})

my session code :

app.use(
session({
  name: COOKIE_NAME,
  // store: new RedisStore({
  //   client: redisClient,
  //   disableTouch: true,
  //   disableTTL: true,
  // }),
  cookie: {
    maxAge: 1000 * 60 * 60 * 24 * 365, //1year
    httpOnly: true,
    sameSite: "lax", //protecting csrf
    // secure:__prod__  //cookie only works in https
    secure:__prod__
  },
  secret: "hellovikash",
  resave: false,
  saveUninitialized: true,
})
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You do not need the expires in the options when clearing the cookie. And the other options must be exactly the same as you used when you set the cookie. So if you, besides domain and path, for example also set secure or httpOnly, you also should add these when clearing the cookie.

See Express docs.

Edit: In your case the it would probably be:

res.clearCookie(COOKIE_NAME, {
    httpOnly: true,
    sameSite: "lax",
    path: "/", // default when setting session cookie
    secure:__prod__
})

But since you are using a session, which sets a session cookie, you could also clear the cookie by destroying the session, using:

req.session.destroy(error =>{
   console.log(error);
});

This might be the preferred way, I think.

Hope this helps.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda