AuthenticationApp / app /dependencies.py
amaye15's picture
Intial Deployment
bfe88a9
raw
history blame
855 Bytes
from fastapi import HTTPException, status, Request # Request may not be needed if token passed directly
from typing import Optional
from . import auth
from .models import User
# This dependency assumes the token is passed somehow,
# e.g., in headers (less likely from Gradio client code) or as an argument
# We will adapt how the token is passed from Gradio later.
async def get_optional_current_user(token: Optional[str] = None) -> Optional[User]:
if token:
user = await auth.get_current_user_from_token(token)
return user
return None
async def get_required_current_user(token: Optional[str] = None) -> User:
user = await get_optional_current_user(token)
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
)
return user