Spaces:
Sleeping
Sleeping
| # app/security.py | |
| from fastapi import Depends, Header, HTTPException, status | |
| from sqlalchemy.orm import Session | |
| from app.db import get_db | |
| from app import models | |
| def get_current_user( | |
| authorization: str = Header(None, alias="Authorization"), | |
| db: Session = Depends(get_db), | |
| ) -> models.User: | |
| """ | |
| Basit token auth: | |
| - Header: Authorization: Bearer <user_id> | |
| - <user_id> int'e çevrilir | |
| - Kullanıcı bulunur | |
| """ | |
| if not authorization: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Missing Authorization header", | |
| ) | |
| try: | |
| scheme, token = authorization.split(" ") | |
| except ValueError: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid Authorization header format", | |
| ) | |
| if scheme.lower() != "bearer": | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Authorization must start with Bearer", | |
| ) | |
| # Token = user_id | |
| try: | |
| user_id = int(token) | |
| except ValueError: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid token format", | |
| ) | |
| user = db.query(models.User).filter_by(id=user_id).first() | |
| if not user: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="User not found", | |
| ) | |
| # ❗ Email doğrulaması artık kaldırıldı → kontrol yok | |
| # ❗ Eğer kullanıcı aktif değilse (bu kuralı istersen tutabiliriz) | |
| if not user.is_active: | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="User account disabled", | |
| ) | |
| return user | |