Backend is Spring boots. I setup cookie there and it is working fine with postman.
@CrossOrigin(origins = "http://localhost:3000", allowCredentials = "true") //Spring boots (http://localhost:8080) Cookie cookie = new Cookie("access-token",token); cookie.setPath("/"); cookie.setSecure(false); response.addCookie(cookie); response.addHeader("Access-Control-Allow-Credentials", "true");
Front End is ReactJS, I’ve used a POST method using Axios to get Login information. I am getting response, but cookies are not set.
//React JS (http://localhost:3000) axios({ url: common.url + '/login', data: "userId=" + userid+ "&password=" + password, method: 'post', cache: false, contentType: 'application/x-www-form-urlencoded', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Cache-Control': 'no-cache','withCredentials':'true' } }).then(function (result) { ; //Success Login })
Tried many things. No Luck
Advertisement
Answer
Got a fix finally.
axios.post(url+'/login', { json }, { withCredentials: true }).then(function (result) { }
WithCredentials should be passed like this from front-end. in Backend code, along with the Crossorigin management, we need to add allowCredentials to true