Question

Windsurf generated an API without any auth, how do I add it afterwards?

Solved · 7 views · asked by dana_ships · edited

The FastAPI backend has 12 endpoints and none check who is calling. Anyone with the URL can list all lessons.

What I’ve tried

Started adding an if not user to each endpoint by hand. Tedious and I already missed two.

Comment

1 answer

Marked as helpful by the asker
olu_backend · edited

Don't do it per endpoint. One dependency, applied to the router:

async def current_user(token: str = Depends(oauth2_scheme)) -> User: ...

router = APIRouter(dependencies=[Depends(current_user)])

Every route under that router now requires auth, including the ones you add next month. Keep a separate router for the two public routes (health, login). Then ask Windsurf to move the endpoints between the two routers; that's a task it does well.

Comment