Frontend guard shows login screen for unauthenticated users. Backend verifies Firebase ID tokens on all protected endpoints. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
24 lines
741 B
Python
24 lines
741 B
Python
import firebase_admin
|
|
from firebase_admin import auth as firebase_auth, credentials
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
|
|
from app.config import settings
|
|
|
|
if settings.firebase_project_id:
|
|
firebase_admin.initialize_app(options={"projectId": settings.firebase_project_id})
|
|
|
|
bearer = HTTPBearer()
|
|
|
|
|
|
async def get_current_user(
|
|
cred: HTTPAuthorizationCredentials = Depends(bearer),
|
|
) -> dict:
|
|
try:
|
|
decoded = firebase_auth.verify_id_token(cred.credentials)
|
|
return decoded
|
|
except Exception:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid or expired token",
|
|
)
|