Spaces:
Sleeping

todo-project / src /api /dependencies.py
bhaimavi353's picture
Upload folder using huggingface_hub
b3943f2 verified
Raw History Blame Contribute Delete
1.08 kB
from typing import Annotated
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlmodel import Session
from src.database import get_session
from src.services import AuthService
from src.models import User
security = HTTPBearer()
async def get_current_user(
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)],
session: Annotated[Session, Depends(get_session)]
) -> User:
token = credentials.credentials
payload = AuthService.decode_token(token)
if not payload:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or expired token",
headers={"WWW-Authenticate": "Bearer"}
)
user_id = int(payload.get("sub"))
user = AuthService.get_user_by_id(session, user_id)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User not found",
headers={"WWW-Authenticate": "Bearer"}
)
return user