Files
dashboard/backend/routes/auth.py
T
yangy 661b85ce62 Enhance backend functionality and frontend UI
- Updated main.py to include authentication for brand, model, and OTA routers.
- Added new OTA schemas in schemas.py for version management.
- Enhanced model retrieval with sorting options in models.py.
- Improved model update functionality to support multipart/form-data uploads.
- Updated frontend layout and styles for a more modern look, including new font integration.
- Implemented login route and authentication checks in router/index.js.
- Added sorting capabilities in model table and improved file handling in model view.
- Updated requirements.txt to include PyJWT for token management.
2026-05-14 17:54:36 +08:00

37 lines
1.1 KiB
Python

import logging
from fastapi import APIRouter
from pydantic import BaseModel, Field
from response import ApiResponse
from security import create_access_token, verify_credentials
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/auth", tags=["auth"])
class LoginBody(BaseModel):
username: str = Field(..., min_length=1, max_length=64)
password: str = Field(..., min_length=1, max_length=128)
@router.post("/login", response_model=ApiResponse)
def login(body: LoginBody):
"""控制台登录,成功后返回 JWT(有效期 12 小时)"""
if not verify_credentials(body.username.strip(), body.password):
logger.warning("Login failed for username=%s", body.username)
return ApiResponse(code=0, msg="用户名或密码错误", data=None)
token = create_access_token()
ttl_seconds = 12 * 60 * 60
logger.info("User %s logged in", body.username.strip())
return ApiResponse(
code=1,
msg="success",
data={
"access_token": token,
"token_type": "bearer",
"expires_in": ttl_seconds,
},
)