Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, status, Depends | |
| from fastapi.responses import JSONResponse | |
| from typing import Annotated | |
| from src.apis.models.user_models import User | |
| from src.apis.controllers.auth_controller import ( | |
| login_control, | |
| ) | |
| from src.apis.middlewares.auth_middleware import get_current_user | |
| from pydantic import BaseModel, Field | |
| from src.utils.logger import logger | |
| router = APIRouter(prefix="/auth", tags=["Authentications"]) | |
| user_dependency = Annotated[User, Depends(get_current_user)] | |
| class LoginRequest(BaseModel): | |
| username: str = Field(..., description="Username") | |
| password: str = Field(..., description="Password") | |
| model_config = { | |
| "json_schema_extra": { | |
| "example": { | |
| "username": "johnUS192", | |
| "password": "1234567890", | |
| } | |
| } | |
| } | |
| async def login(body: LoginRequest): | |
| try: | |
| logger.info(f"User {body.username} is logging in.") | |
| token, user_data = await login_control(body.username, body.password) | |
| return JSONResponse( | |
| content={ | |
| "token": token, | |
| "user_data": user_data, | |
| }, | |
| status_code=200, | |
| ) | |
| except Exception as e: | |
| return JSONResponse(content={"message": str(e)}, status_code=500) | |