DEV Community

Mao Le
Mao Le

Posted on

Express doesn't save cookie

Although the response header has 'Set-Cookie: ...' but I didn't see anything at Application -> Cookie in Developer tool of the browser. So I fixed something at the backend (express) and frontend (React and axios) too.

Step 1: Update configuration credentials at Backend side

Currently, I was use the cors() package. So I update credentials for cors()

const app = express();

// Setting credentials
app.use(cors({
  origin: 'http://localhost:3000', // it depends on your site
  credentials: true,
}));

// Setting cookies
res.cookie('cookie-for-me', 'hello-world');
Enter fullscreen mode Exit fullscreen mode

Step 2: Update configuration credentials at Backend side

It almost done after this step.

axios.defaults.withCredentials = true
Enter fullscreen mode Exit fullscreen mode

Final: Run and checking your cookie

You can check by enter document.cookie at the console of the developer tool of a browser to see the result.

If you have any suggestion about the way saving the cookie, please feel free comment as the following the post :)

Oldest comments (0)