Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

90
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda