express - Node.Js: Signed cookie can't be found -
using mean environment (with express 4), create cookie this.
//webserver.js app.use(cookieparser(„somesecretkey“)); //somescript.js res.cookie(‚testcookie‘, ‚testvalue', {signed: true, maxage: 999999, httponly: true});
in script, try check existence of cookie this.
//someotherscript.js if(req.cookies.testcookie){ console.log("cookie exists“+req.cookies.testcookie); }else{ console.log(„no cookie“+req.cookies.testcookie); //always undefined }
i checked browser cookie , exists console keeps logging there no cookie (cookie undefined) when press refresh or visit page. change cookie unsigned , remove secret key, can access it!? why can’t cookie found once signed?
the expressjs documentation res.cookie tells us:
when using cookie-parser middleware, method supports signed cookies. include
signed
option settrue
.res.cookie()
use secret passedcookieparser(secret)
sign value.res.cookie('name', 'tobi', { signed: true });
later may access value through req.signedcookie object.
so:
- did specific secret using
cookieparser
? - you should check cookie in
req.signedcookie
, notreq.cookies
Comments
Post a Comment