from fastapi import Depends, HTTPException, status from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer from firebase_admin import auth from firebase_admin.auth import ExpiredIdTokenError, InvalidIdTokenError from app import logger from . import db security = HTTPBearer() def get_current_user( credentials: HTTPAuthorizationCredentials = Depends(security), ): try: payload = auth.verify_id_token(credentials.credentials) user_doc_ref = db.collection("user").document(payload["sub"]).get() if not user_doc_ref.exists: raise HTTPException(status_code=400, detail="User profile not found") except ExpiredIdTokenError as e: logger.warning(e) raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Token expired", headers={"WWW-Authenticate": "Bearer"}, ) except InvalidIdTokenError as e: logger.warning(e) raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token", headers={"WWW-Authenticate": "Bearer"}, ) except ValueError as e: logger.warning(e) raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token", headers={"WWW-Authenticate": "Bearer"}, ) return payload