🐍 FastAPI 서버 코드 main.py
@app.post("/login")
async def login(username: str = Form(...)):
sid = secrets.token_hex(16)
session_store[sid] = {"user": username}
resp = JSONResponse({"ok": True})
resp.set_cookie(
key="session_id", value=sid,
httponly=True,
secure=True,
samesite="lax",
)
return resp
@app.get("/mypage")
async def mypage(request: Request):
sid = request.cookies.get("session_id")
session = session_store.get(sid)
if not session:
raise HTTPException(401)
return {"user": session["user"]}