File size: 2,137 Bytes
f40c733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from typing import Optional
from sqlmodel import select
from datetime import datetime

from ..utils.jwt_utils import decode_access_token
from ..models.user import User, UserRead
from ..database.database import get_async_session
from sqlmodel.ext.asyncio.session import AsyncSession

security = HTTPBearer()


async def get_current_user(
    credentials: HTTPAuthorizationCredentials = Depends(security),
    session: AsyncSession = Depends(get_async_session)
) -> UserRead:
    """
    Dependency to get the current authenticated user from the JWT token.

    Args:
        credentials: HTTP authorization credentials from the request
        session: Async database session

    Returns:
        UserRead object if token is valid and user exists

    Raises:
        HTTPException: If token is invalid, expired, or user doesn't exist
    """
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": "Bearer"},
    )

    token = credentials.credentials

    payload = decode_access_token(token)
    if payload is None:
        raise credentials_exception

    user_id: Optional[str] = payload.get("sub")
    if user_id is None:
        raise credentials_exception

    # Fetch the user from the database
    user = await session.get(User, int(user_id))
    if user is None:
        raise credentials_exception

    # Create UserRead object manually to ensure required fields are present
    # This handles potential issues with datetime fields being None
    from datetime import datetime
    created_at = user.created_at or datetime.utcnow()
    updated_at = user.updated_at or datetime.utcnow()

    return UserRead(
        id=user.id,
        email=user.email,
        name=user.name,
        date_of_birth=user.date_of_birth,
        signup_date=user.signup_date,
        force_password_change=user.force_password_change,
        created_at=created_at,
        updated_at=updated_at
    )