Search code examples
node.jsamazon-web-servicesgoogle-chromecors

Cookies saved in Postman but not in browser(Next.js) after uploading to AWS EC2


I have been trying to solve this issue for the past two days already, but still, no success. None of the answers provided here have resolved my issue regarding cookies in the response in the browser.

I have Node.js (Express.js) code which works fine on my local machine localhost:8088. I receive cookies in the browser when the user logs in.

However, after uploading my code to an AWS EC2 nginx server, I am not able to get cookies in my browser, although I am able to receive them in Postman. Can someone assist me with this issue?

Express.js

const attachCookiesToResponse = ({ res, user, refreshToken }) => {
  const accessTokenJWT = createJWT({ payload: { user } })
  const refreshTokenJWT = createJWT({ payload: { user, refreshToken } })

  const oneDay = 24 * 60 * 60 * 1000
  const oneWeek = oneDay * 7

  res.cookie('accessToken', accessTokenJWT, {
    httpOnly: true,
    secure: process.env.NODE_ENV === 'production',
    signed: true,
    expires: new Date(Date.now() + oneDay)
  })

  res.cookie('refreshToken', refreshTokenJWT, {
    httpOnly: true,
    secure: process.env.NODE_ENV === 'production',
    signed: true,
    expires: new Date(Date.now() + oneWeek)
  })
}

Next.js

export const requestLogin = async (body: unknown) => {
  try {
    const response = await axios.post(apiEndpoint.login(), body, {
      withCredentials: true,
    });
    return response;
  } catch (error) {
    return error;
  }
};

Solution

  • Problem was resolved after getting SSL certificate.