We use CORS + SameSite to prevent CSRF attack on the server side & Next.js build a static site with Apache mod_proxy, the Domain component is handled by Apache's ProxyPassReverseCookieDomain directive.
This CSRF protection breaks local dev-server. Initially we configured rewrites in next.config.js:
if (process.env.BACKEND_API) {
nextConfig.rewrites = async () => {
return [
{
source: '/api/:path*',
destination: `${process.env.BACKEND_API}/:path*`
}
]
}
}
But now we need middleware to rewrite Set-Cookie Domain from one name to the localhost...
Is it possible to define middleware action on cookies only for dev-server?
Here is a middleware located in
src/middleware.tsthat modifies the Domain attribute:This solution, however, presents two disadvantages: