Syed Faraz Hasan
Syed Faraz Hasan

Reputation: 11

Cookies not being saved in browser

I am using React and Express I am sending cookies using res.cookie, which are being received by the browser, but they are not being saved in the browser.

This is my code for generating and sending the cookie

const jwtToken = jwt.sign(
      {
        id: user._id, email: user.email,
      },
      process.env.SECRET_KEY,
      {
        expiresIn: "1d",
      }
    );

    res.cookie("accessToken", jwtToken, {
      httpOnly: true,
      sameSite: "None",
      secure: process.env.NODE_ENV === "production",
      maxAge: 4 * 60 * 60 * 1000,
      path: "/",
    });

This cookie is also available in the response headers for the login endpoint Cookie response in header

However, when I use the verify endpoint, which has authenticate middleware to extract user from the accessToken, it is not receiving the "accessToken" cookie.

Under the application section, the accessToken cookie is not being saved.

This is my verify user request

const config = {
      url: `${process.env.REACT_APP_BACKEND_LINK}/auth/verify`,
      method: "get",
      withCredentials: true,
    };

I tried on localhost and on hosted platform as well, I don't understand what the issue is

Upvotes: 0

Views: 35

Answers (0)

Related Questions