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 set true. res.cookie() use secret passed cookieparser(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, not req.cookies

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -