from fastapi import Depends, HTTPException, status from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from sqlalchemy.orm import Session from app.db.session import get_db from app.models.user import User from app.core.security import verify_token security = HTTPBearer() async def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security), db: Session = Depends(get_db)): credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) token = credentials.credentials email = verify_token(token) if email is None: raise credentials_exception user = db.query(User).filter(User.email == email).first() if user is None: raise credentials_exception return user async def get_current_active_user(current_user: User = Depends(get_current_user)): if not current_user.is_active: raise HTTPException(status_code=400, detail="Inactive user") return current_user async def get_current_producer(current_user: User = Depends(get_current_active_user)): if not current_user.is_producer: raise HTTPException(status_code=403, detail="Not a producer") return current_user