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,
})
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.
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.