45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
# app/api/deps.py
|
|
from typing import Generator, Optional
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.database import SessionLocal, get_db
|
|
from app.config import settings
|
|
|
|
# Re-export get_db для удобства
|
|
__all__ = ["get_db", "get_current_user", "get_current_active_user"]
|
|
|
|
security = HTTPBearer(auto_error=False)
|
|
|
|
|
|
def get_current_user(
|
|
credentials: Optional[HTTPAuthorizationCredentials] = Depends(security)
|
|
) -> Optional[dict]:
|
|
"""
|
|
Dependency для получения текущего пользователя (опционально)
|
|
"""
|
|
if not credentials:
|
|
return None
|
|
|
|
# Здесь будет логика проверки JWT токена
|
|
# Пока возвращаем тестового пользователя
|
|
if settings.APP_ENV == "development":
|
|
return {"id": 1, "username": "test_user", "email": "test@example.com"}
|
|
|
|
return None
|
|
|
|
|
|
def get_current_active_user(
|
|
current_user: Optional[dict] = Depends(get_current_user)
|
|
) -> dict:
|
|
"""
|
|
Dependency для получения активного пользователя (требует аутентификации)
|
|
"""
|
|
if not current_user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Not authenticated",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
return current_user |