38 lines
738 B
Python
38 lines
738 B
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from db.session import get_db
|
|
|
|
from domains.auth.schemas import (
|
|
LoginRequest,
|
|
TokenResponse
|
|
)
|
|
|
|
from domains.auth.service import login_user
|
|
|
|
|
|
router = APIRouter(
|
|
prefix="/auth",
|
|
tags=["auth"]
|
|
)
|
|
|
|
|
|
@router.post("/login", response_model=TokenResponse)
|
|
async def login(
|
|
payload: LoginRequest,
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
|
|
token = await login_user(
|
|
db,
|
|
payload.username,
|
|
payload.secret
|
|
)
|
|
|
|
if not token:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid username or secret"
|
|
)
|
|
|
|
return token |