javascript - ExpressJS Cookie-Parser not persisting between different HTTP calls -


i using cookie - parser express.js. in express config file have app.use(cookieparser()) , in main (app.js) server set cookie whenever there post request /signin , username , password both equal.

app.post("/signin",(req,res)=>{     log(`checking .....`);     const req_data=req.body;     log(req.body);     if(req.body.user===req.body.password) {         res.cookie.level="recruiter";//i tried doing res.cookie("level","recruiter")         res.send({redirect:true,redirect_url:"\\"+res.cookie.level})     } else {         log(`fishy..!`);         res.send({"authenticated":false});     } }); 

now when client receives part of fetch api response extracts redirect_url part of json , below:

fetch("/signin",{     method:"post",     headers: {         'accept': 'application/json',         'content-type': 'application/json'     },     body:json.stringify(json) }) .then((res)=>{     log(res);     return res.json(); }) .then((res)=>{     if(res["authenticated"] == false) {         $("#credential_alert").style="display:block;";         settimeout(function(){             $("#credential_alert").style="display:none;";         },1000);         $("#user").value="";         $("#password").value="";     } else if(res["redirect"]===true && res["redirect_url"]) {         window.location.href=res["redirect_url"];     } }) 

now real issue when in route trying access http cookie set in /signin route using below code..

app.get("/recruiter",(req,res)=>{     log(req.cookies.level);//coming out undefined     res.render("recruiter.ejs"); }); 

as can see, level res.cookies coming out undefined don't know why.

answer got answer, fetch api not send credentials default it's request have set credentials:true send credentials along fetch api's call.

hi you're setting cookie request means(by defualt httponly :true) you've set cookie res.cookie("level","recruiter",{httponly:false,maxage:/optional/})


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -