I have deployed my front-end on Vercel and back-end on railway. Localy it works fine, but in production I have a problem with non-storing cookies.
Here is my axios config for front-end (Next.js):
export const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_SERVER_URL,
headers: {
'Content-Type': 'application/json'
},
withCredentials: true
})
Here is my config for back-end (Nest.js):
app.use(cookieParser());
app.enableCors({
origin: config.getOrThrow<string>('ALLOWED_ORIGIN'),
credentials: true,
exposedHeaders: ['set-cookie'],
});
app.use((req: Request, res: Response, next: NextFunction) => {
res.setCookie = (name: string, value: string, options = {}) => {
res.cookie(name, value, {
httpOnly: true,
secure: config.getOrThrow<string>('NODE_ENV') === 'production', //(it's true on production)
sameSite: 'none',
...options,
});
};
next();
});