I’m having trouble with cookies in my deployed app. The frontend is on Netlify and the backend on Railway. It works fine locally but not in production.
The main problems are:
- Cookies aren’t being set in the browser. I get a message saying it’s blocked due to user preference.
- API calls don’t include the cookie.
My setup:
Backend (Express):
app.use(cors({
origin: process.env.FRONTEND_URL || 'http://localhost:5173',
credentials: true,
}));
res.cookie('authToken', `Bearer ${generatedToken}`, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
maxAge: 7200000,
sameSite: 'none',
});
Frontend (React with RTK Query):
const apiQuery = fetchBaseQuery({
baseUrl: import.meta.env.VITE_API_URL,
credentials: "include",
});
Environment setup:
FRONTEND_URL: https://my-app-frontend.netlify.app
VITE_API_URL: https://my-app-backend.up.railway.app
Login works and creates a token but it’s not sent with other API requests. I get a 401 error even though the token exists. The token disappears on page reload too.
I’ve tried Chrome, Safari, and Edge. They all have the same issue.
Why are cookies being blocked? How can I make sure they work properly in production for cross-origin requests?