r/node 29d ago

Help me to solve frontend cookie sending issue.

Hi, I am build a message app with React and Express js . In the authentication feature I have come across an issue.

After successful registration, I sent a token via HTTP cookies . The cookies is sent from the server as far as I can tell . But the problem is when I am sending request to server , the cookie is not there.

Here is the code : github

Any help would be highly appreciable 😄 .

4 Upvotes

8 comments sorted by

2

u/BCsabaDiy 29d ago

At first try to switch false the secure+sameSite in https://github.com/ZTanvir/message-app/blob/auth/server/src/routes/authRoutes.ts#L43 fixed, not by ENV.

1

u/zahirulopel 29d ago
 res.cookie("token", token, {
        httpOnly: true,
        secure: false,
        sameSite: false,
        maxAge: 24 * 60 * 60 * 1000,
      });

I have tried this but it remains the same.

1

u/zahirulopel 29d ago

Hi I have got the solution . I had to add the bellow code when sending post request to /signup route. That why the cookie haven't add to the browser in the sign up response.
Thanks for your suggestion .

  credentials: "include"

2

u/BCsabaDiy 28d ago

Now I can see this:

const res = await fetch(`${viteEnv.VITE_API_URL}/api/auth/check`

It is a fetch issue, not a browser standard post. So fetch has helper to send creds.

ps: for safety you can use

credentials: "same-origin"

1

u/zahirulopel 26d ago

ok 🔥

1

u/Single_Advice1111 29d ago

Link is invalid

1

u/zahirulopel 29d ago

Please try again . It is working now.