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

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

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 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!