refactor: improve code structure and optimize key functions
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.security import create_access_token
|
||||
from app.schemas.user import Token
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
username: str = Field(min_length=1)
|
||||
password: str = Field(min_length=1)
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/login", response_model=Token)
|
||||
async def login_for_access_token(payload: LoginRequest) -> Token:
|
||||
if not (payload.username == "admin" and payload.password == "admin"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
)
|
||||
|
||||
access_token = create_access_token(
|
||||
subject="00000000-0000-0000-0000-000000000001",
|
||||
role="PM",
|
||||
expires_delta=timedelta(minutes=settings.JWT_EXPIRE_MINUTES),
|
||||
)
|
||||
return Token(access_token=access_token, token_type="bearer")
|
||||
Reference in New Issue
Block a user