|
| 1 | +""" |
| 2 | +Frontend-facing auth + profile API (React-ready). |
| 3 | +
|
| 4 | +The React app authenticates the user with the auth provider (Supabase by default) |
| 5 | +and sends its JWT as `Authorization: Bearer <token>`. We verify it, get-or-create |
| 6 | +the matching control-plane user, and expose the endpoints the UI needs: |
| 7 | +
|
| 8 | + GET /me -> current user + their profiles |
| 9 | + GET /profiles -> list profiles |
| 10 | + POST /profiles -> create a profile (self / spouse / ...) |
| 11 | +
|
| 12 | +Provider-agnostic: swap verify_token() for Firebase/Clerk if you change providers. |
| 13 | +""" |
| 14 | +import os |
| 15 | +from typing import Optional |
| 16 | +from fastapi import APIRouter, Depends, Header, HTTPException |
| 17 | +from pydantic import BaseModel |
| 18 | + |
| 19 | +router = APIRouter() |
| 20 | + |
| 21 | + |
| 22 | +def verify_token(token: str) -> dict: |
| 23 | + """Verify a Supabase JWT (HS256) and return {id, email}. Fail closed.""" |
| 24 | + secret = os.getenv("SUPABASE_JWT_SECRET") |
| 25 | + if not secret: |
| 26 | + raise HTTPException(status_code=503, detail="Auth is not configured") |
| 27 | + import jwt # lazy: only needed when actually verifying |
| 28 | + try: |
| 29 | + claims = jwt.decode(token, secret, algorithms=["HS256"], audience="authenticated") |
| 30 | + except Exception: |
| 31 | + raise HTTPException(status_code=401, detail="Invalid or expired token") |
| 32 | + return {"id": claims.get("sub"), "email": claims.get("email")} |
| 33 | + |
| 34 | + |
| 35 | +def get_current_user(authorization: Optional[str] = Header(None)) -> dict: |
| 36 | + """FastAPI dependency: verify the bearer token and resolve our control-plane user.""" |
| 37 | + if not authorization or not authorization.lower().startswith("bearer "): |
| 38 | + raise HTTPException(status_code=401, detail="Missing bearer token") |
| 39 | + claims = verify_token(authorization.split(" ", 1)[1]) |
| 40 | + email = claims.get("email") |
| 41 | + if not email: |
| 42 | + raise HTTPException(status_code=401, detail="Token has no email") |
| 43 | + |
| 44 | + from app.core import identity |
| 45 | + user = identity.get_user_by_email(email) |
| 46 | + if not user: |
| 47 | + identity.create_user(email) |
| 48 | + user = identity.get_user_by_email(email) |
| 49 | + return {"user_id": user.id, "email": email} |
| 50 | + |
| 51 | + |
| 52 | +def _profile_dto(p): |
| 53 | + return {"id": p.id, "display_name": p.display_name, "relation": p.relation, |
| 54 | + "itr_type": p.itr_type, "drive_folder_id": p.drive_folder_id} |
| 55 | + |
| 56 | + |
| 57 | +@router.get("/me") |
| 58 | +def me(user: dict = Depends(get_current_user)): |
| 59 | + from app.core import identity |
| 60 | + profiles = identity.list_profiles(user["user_id"]) |
| 61 | + return {"user": user, "profiles": [_profile_dto(p) for p in profiles]} |
| 62 | + |
| 63 | + |
| 64 | +@router.get("/profiles") |
| 65 | +def list_profiles(user: dict = Depends(get_current_user)): |
| 66 | + from app.core import identity |
| 67 | + return [_profile_dto(p) for p in identity.list_profiles(user["user_id"])] |
| 68 | + |
| 69 | + |
| 70 | +class ProfileIn(BaseModel): |
| 71 | + display_name: str |
| 72 | + relation: str = "self" # self | spouse | dependent | other |
| 73 | + itr_type: str = "ITR1" |
| 74 | + drive_folder_id: str = "" |
| 75 | + |
| 76 | + |
| 77 | +@router.post("/profiles") |
| 78 | +def create_profile(body: ProfileIn, user: dict = Depends(get_current_user)): |
| 79 | + from app.core import identity |
| 80 | + pid = identity.create_profile( |
| 81 | + user["user_id"], body.display_name, relation=body.relation, |
| 82 | + itr_type=body.itr_type, drive_folder_id=body.drive_folder_id) |
| 83 | + return {"id": pid} |
0 commit comments