Cookie doesn't send to browser

Hi everyone,

I’m deploying my MERN product to render.com. I deploy the frontend and backend separately, below is the links,
Frontend: https://yard-hnyg.onrender.com
Backend: https://yard-api.onrender.com

I implement the authentication with cookies, it works on local but doesn’t work after deployment. I spend 2 days and I still don’t know how to fix it, please let me know if this a platform issue or is there any workaround that I could do. Thanks!
Will it help if I deploy my project both frontend and backend together?

here is my setup

// server.js
app.set("trust proxy", 1)
app.use(
  cors({
    origin: "https://yard-hnyg.onrender.com",
    credentials: true,
    optionsSuccessStatus: 200,
  })
)

const MemoryStore = createMemoryStore(session)
app.use(
  session({
    secret: "keyboard cat",
    cookie: {
      httpOnly: true,
      expires: new Date(Date.now() + 1000 * 60 * 60 * 24),
      secure: process.env.NODE_ENV === "production",
      signed: true,
    },
    store: new MemoryStore({
      checkPeriod: 24 * 60 * 60,
    }),
    resave: false,
    saveUninitialized: false,
  })
)

This is how I attach cookies

const attachCookiesToResponse = (res, payload) => {
  const token = createJWT(payload)
  const oneDay = 1000 * 60 * 60 * 24

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

Hey there,
One thing at play here might be the onrender.com, it’s a listed public suffix so cookies can’t be shared across different subdomains - if you were to use your own sub-domain you might find the issue is resolved,

Let us know,

Regards,

John B

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.