from fastapi import Depends, HTTPException, status, Request from sqlmodel import Session from typing import Generator from ..database import get_session_dep from ..models.user import User from .security import verify_user_id_from_token from uuid import UUID def get_current_user( request: Request, session: Session = Depends(get_session_dep) ) -> User: """Dependency to get the current authenticated user from JWT token in cookie.""" # Debug: Print all cookies print(f"All cookies received: {request.cookies}") # Get the token from the cookie token = request.cookies.get("access_token") print(f"Access token from cookie: {token}") if not token: print("No access token found in cookies") raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Not authenticated", headers={"WWW-Authenticate": "Bearer"}, ) user_id = verify_user_id_from_token(token) print(f"User ID from token: {user_id}") if not user_id: print("Invalid user ID from token") raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authentication credentials", headers={"WWW-Authenticate": "Bearer"}, ) user = session.get(User, user_id) print(f"User from database: {user}") if not user: print("User not found in database") raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authentication credentials", headers={"WWW-Authenticate": "Bearer"}, ) return user def get_user_by_id( user_id: UUID, session: Session = Depends(get_session_dep) ) -> User: """Dependency to get a user by ID from the database.""" user = session.get(User, user_id) if not user: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="User not found" ) return user