todo_app / src /auth.py
MuzammilMax's picture
Upload folder using huggingface_hub
ed5cf78 verified
Raw
History Blame Contribute Delete
1.42 kB
from datetime import datetime
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from src.database import get_session
from src.models import User, Session
security = HTTPBearer()
async def get_current_user(auth: HTTPAuthorizationCredentials = Depends(security), session: AsyncSession = Depends(get_session)) -> User:
token = auth.credentials
# Query Better Auth session table
stmt = select(Session).where(Session.token == token)
result = await session.execute(stmt)
db_session = result.scalars().first()
if not db_session:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid session token",
headers={"WWW-Authenticate": "Bearer"},
)
if db_session.expiresAt < datetime.utcnow():
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Session expired",
headers={"WWW-Authenticate": "Bearer"},
)
user = await session.get(User, db_session.userId)
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User not found",
headers={"WWW-Authenticate": "Bearer"},
)
return user