Thursday 19 April 2018

Setting cookie for localhost from remote node server

I have the client code running on my localhost and a nodejs server on heroku.I send a request to node server and set a cookie accordinglyWhen both server and client lies on the localhost, I can set cookies by using set-cookie-parser package.However, it seems like I cannot set the cookie when the node server runs on remote heroku. I am sure the AJAX call is caught by the server.Is there anything special I have to do in that case or the problem lies somewhere different?Client:axios.post("https://ift.tt/2Hb1rKq", {email, password}, {withCredentials: true}) Server:let registerOptions = { url: 'https://ift.tt/2Hb1rKq', method: 'POST', form: {} }; app.post("/register", (req, res) => { registerOptions.form = req.body; request(registerOptions, (error, response, body) => registerCallback(res, error, response, body)) }) const registerCallback = (res, error, response, body) => { if(response.statusCode === 200){ cookieData = setCookie.parse(response.caseless.dict['set-cookie'][0]); const {name, value, path, secure, httpOnly} = cookieData[0]; options = {path, secure, httpOnly}; res.cookie(name, value).send('CookieSet') } else{ res.status(response.statusCode).send(response.body); } }

Submitted April 19, 2018 at 10:44AM by eligloys

No comments:

Post a Comment