skill-forge/API/app/controllers/auth_router.py

29 lines
760 B
Python

from fastapi import APIRouter, HTTPException
from fastapi.params import Depends
from sqlalchemy.orm import Session
from app.database.dependencies import get_db
from app.domain.entities.auth_entity import AuthEntity
from app.domain.entities.token_entity import TokenEntity
from app.infrastructure.auth_service import AuthService
router = APIRouter()
@router.post("/login/", response_model=TokenEntity)
def login(
auth_data: AuthEntity,
db: Session = Depends(get_db)
):
auth_service = AuthService(db)
token = auth_service.authenticate(auth_data.login, auth_data.password)
if token is None:
raise HTTPException(
status_code=401,
detail="Incorrect username or password"
)
return token