digitros / nextjs-fastapi

https://nextjs-fastapi-starter.vercel.app
MIT License
408 stars 117 forks source link

Setting CORS for the FastAPI server to be accessed only be the NextJS frontend #17

Open qu8n opened 4 months ago

qu8n commented 4 months ago

Is it possible to enable CORS on the FastAPI server so that only the NextJS frontend can make requests to it?

I've tried the standard way below, which didn't work. Could the fix involve modifying the Next rewrite configs?

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['http://localhost:3000', 'https://helloworld.vercel.app'],
    allow_methods=["*"],
    allow_headers=["*"],
)

...