from fastapi import HTTPException, status from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from jose import JWTError, jwt from functools import wraps import os from dotenv import load_dotenv load_dotenv() # JWT Configuration SECRET_KEY = os.getenv("JWT_SECRET", "your-super-secret-jwt-key-change-in-production") ALGORITHM = "HS256" security = HTTPBearer() def verify_token(token: str): """ Verify and decode a JWT token """ try: payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) user_id: int = payload.get("sub") if user_id is None: raise HTTPException(status_code=401, detail="Could not validate credentials") return user_id except JWTError: raise HTTPException(status_code=401, detail="Could not validate credentials") def get_current_user(credentials: HTTPAuthorizationCredentials = security): """ Dependency to get current authenticated user """ user_id = verify_token(credentials.credentials) return user_id