Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!