Spaces:
Sleeping
Sleeping
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from sqlalchemy.orm import Session | |
| from app.db.database import get_db | |
| from app.auth.jwt_handler import decode_token | |
| from app.db.crud import get_user_by_id | |
| security = HTTPBearer() | |
| def get_current_user( | |
| credentials: HTTPAuthorizationCredentials = Depends(security), | |
| db: Session = Depends(get_db) | |
| ): | |
| token = credentials.credentials | |
| payload = decode_token(token) | |
| if not payload: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Token invalide ou expiré" | |
| ) | |
| user_id = int(payload.get("sub", 0)) | |
| user = get_user_by_id(db, user_id) | |
| if not user: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Utilisateur non trouvé" | |
| ) | |
| return user |