admin管理员组文章数量:1202195
Basically i'm doing redirect from a.example to www.example and i expect to be able to delete cookies on www.example (because cookie is created with .example as the cookie domain), but following code doesn't work.
I know that this question seems like duplicate question, i tried everything from similar question but it doesn't work. See after the code what i already tried.
Using express 3.0.3 and node 0.10.32.
express session middleware
...
var cookiedata = {
domain : '.example',
originalMaxAge : null,
httpOnly : false
};
app.use(express.session({
store : ...,
secret : ...,
key : 'express.sid',
cookie : cookiedata
}));
...
logout function
function logout(req, res){
...
req.session.destroy(function(){
req.session = null;
res.clearCookie('express.sid', { path: '/' });
res.redirect('');
});
}
What i already tried from similar question
So i put path : '/'
in express session middleware such as:
app.use(express.session({ ..., path : '/' });
No success.
- !topic/express-js/PmgGMNOzhgM
Instead res.clearCookie i used: res.cookie('express.sid', '', {expires: new Date(1), path: '/' });
No success.
Basically i'm doing redirect from a.example.com to www.example.com and i expect to be able to delete cookies on www.example.com (because cookie is created with .example.com as the cookie domain), but following code doesn't work.
I know that this question seems like duplicate question, i tried everything from similar question but it doesn't work. See after the code what i already tried.
Using express 3.0.3 and node 0.10.32.
express session middleware
...
var cookiedata = {
domain : '.example.com',
originalMaxAge : null,
httpOnly : false
};
app.use(express.session({
store : ...,
secret : ...,
key : 'express.sid',
cookie : cookiedata
}));
...
logout function
function logout(req, res){
...
req.session.destroy(function(){
req.session = null;
res.clearCookie('express.sid', { path: '/' });
res.redirect('https://www.example.com');
});
}
What i already tried from similar question
- https://github.com/strongloop/express/issues/691
So i put path : '/'
in express session middleware such as:
app.use(express.session({ ..., path : '/' });
No success.
- https://groups.google.com/forum/#!topic/express-js/PmgGMNOzhgM
Instead res.clearCookie i used: res.cookie('express.sid', '', {expires: new Date(1), path: '/' });
No success.
Share Improve this question edited Aug 20, 2015 at 10:07 Srle asked Aug 20, 2015 at 9:36 SrleSrle 10.5k9 gold badges35 silver badges67 bronze badges 1- 1 Did you find a solution to this? No accepted answer here. – Samuel Méndez Commented Jul 7, 2017 at 9:24
3 Answers
Reset to default 11This is response.clearCookie of Express.JS (file response.js at line 749).
var opts = merge({ expires: new Date(1), path: '/' }, options);
return this.cookie(name, '', opts);
If you set a breakpoint at this line you will see expires is reported at an invalid date. So instead of using response.clearCookie, just make it expire immediately like this one.
response.cookie("express.sid", "", { expires: new Date() });
This is working for me with cookie-parser
module:
router.get('/logout', function(req, res){
cookie = req.cookies;
for (var prop in cookie) {
if (!cookie.hasOwnProperty(prop)) {
continue;
}
res.cookie(prop, '', {expires: new Date(0)});
}
res.redirect('/');
});
What worked for me was adding path and domain in res.clearCookie
res.clearCookie(<cookie-name>, {path: '/', domain: <domain-on-which-cookie-is-set>}
Also, make sure to include credentials on the frontend, otherwise no cookie will be sent with the request. If no cookie goes to the server, it has nothing to clear!
fetch('url.com', {credentials: "include"}
本文标签: javascriptnode expresshow to clear cookie after log outStack Overflow
版权声明:本文标题:javascript - node express, how to clear cookie after log out - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738562303a2099471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论