I've figured the problem. Instead of:
export const middleware = async (req: NextRequest) => {
const origin = req.nextUrl.origin;
if (!publicEnv.CORS_WHITELIST?.includes(origin)) {
return NextResponse.json({ error: `Access denied. Environment: ${process.env.NODE_ENV}. Your Origin: ${origin} | Whitelist: ${publicEnv.CORS_WHITELIST}` }, { status: 405 })
}
...
I've done:
export const middleware = async (req: NextRequest) => {
const host = req.headers.get("host");
const protocol = process.env.NODE_ENV === "production" ? "https" : "http";
const origin = `${protocol}://${host}`;
if (!origin || !publicEnv.CORS_WHITELIST?.includes(origin)) {
return NextResponse.json({ error: `Access denied. Environment: ${process.env.NODE_ENV}. Your Origin: ${origin} | Whitelist: ${publicEnv.CORS_WHITELIST}` }, { status: 405 })
}
...
Also who down voted the post at first publish without a reason? lol.