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
Can't destroy cookie in JavaScript created by Express

For an OAuth flow, I am setting the access token in a cookie on the server (Express) like so:

res.cookie('name' , 'myname', {httpOnly: false, path:'/'});
res.cookie('access_token', accessToken);

// on the client
console.log(document.cookie); // displays "name=myname; access_token=12345"

When/if the access token is no longer valid, I want to destroy the cookie. So on the client, if the server fails to make a successful request with that access token I am doing this:

document.cookie = 'name=myname; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/';
console.log(document.cookie) // displays "access_token=12345"

So for some reason it's removing the "name" portion of the cookie but not the access token. How can I destroy the cookie entirely?

I saw other comments about how the path should be the same or that httpOnly set to true would prevent javascript from mutating the cookie but I've addressed all that and still the token sticks around in the cookie.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use below code:

var delete_cookie = function(name) {
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
delete_cookie('name');
delete_cookie('access_token');
about 4 years ago · Santiago Trujillo Relatório

0

To set cookie you can use

res.cookie("key", value);

To delete cookie you can use

res.clearCookie("key");
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