rswag-online/backend/app/api/admin/auth.py

37 lines
931 B
Python

"""Admin authentication endpoints."""
from fastapi import APIRouter, HTTPException, Depends
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.services.auth_service import AuthService
router = APIRouter()
class LoginRequest(BaseModel):
email: str
password: str
class LoginResponse(BaseModel):
access_token: str
token_type: str = "bearer"
def get_auth_service(db: AsyncSession = Depends(get_db)) -> AuthService:
return AuthService(db)
@router.post("/login", response_model=LoginResponse)
async def login(
request: LoginRequest,
auth_service: AuthService = Depends(get_auth_service),
):
"""Admin login."""
token = await auth_service.authenticate(request.email, request.password)
if not token:
raise HTTPException(status_code=401, detail="Invalid credentials")
return LoginResponse(access_token=token)