79298319

Date: 2024-12-20 20:21:32
Score: 4.5
Natty:
Report link

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.

Reasons:
  • RegEx Blacklisted phrase (2): down vote
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Viserion Wick