DEV Community

Mao Le
Mao Le

Posted on

3 3

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 :)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay