Search code examples
reactjsexpressmernexpress-session

cookies not saving when using express-session


I am using express-session to save cookies into my browser so I can use it for authentication. I am sending a post request from my frontend (React) using axios to the backend and use User.register to save the new user to the database(mongodb)

I am able to save the user to my database however, there are no cookies being saved in my browser.

Frontend post request using axios:

enter image description here

backend post request handler

enter image description here


Solution

  • In your server add this code

      app.use(cors({ credentials: true, origin: "http://localhost:3000" }));
    

    Client axios config

      const Axios= axios.create({
        baseURL: "http://localhost:3001/",
        withCredentials: true,
    });