Spaces:
Runtime error
Runtime error
File size: 1,377 Bytes
f82e38a aaae345 f82e38a aaae345 f82e38a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from typing import Optional
from auth.jwt import verify_token
from models import User
from sqlmodel import Session
from db import get_session
from utils.errors import InvalidTokenError, ValidationError
import uuid
security = HTTPBearer()
def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security),
session: Session = Depends(get_session)
):
"""Get current user from token with additional validation"""
token = credentials.credentials
try:
payload = verify_token(token)
except HTTPException:
raise InvalidTokenError()
user_id_str = payload.get("sub")
if user_id_str is None:
raise InvalidTokenError()
# Convert string UUID to proper UUID object for SQLModel
try:
user_id = uuid.UUID(user_id_str)
except ValueError:
raise InvalidTokenError()
user = session.get(User, user_id)
if user is None:
raise InvalidTokenError()
# Additional validation: check if user is active
# (Assuming there's an 'is_active' field on the User model)
# For now, we'll just return the user since the current User model doesn't have an active field
# In a real implementation, we would check user account status here
return user |