31 lines
902 B
Python
31 lines
902 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||
|
|
from pydantic import BaseModel
|
||
|
|
|
||
|
|
from app.db.platform_store import get_platform_store
|
||
|
|
from app.modules.auth.deps import get_current_user
|
||
|
|
from app.modules.auth.service import create_access_token
|
||
|
|
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
class LoginBody(BaseModel):
|
||
|
|
username: str
|
||
|
|
password: str
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/login")
|
||
|
|
def login(body: LoginBody) -> dict:
|
||
|
|
user = get_platform_store().login(body.username, body.password)
|
||
|
|
if not user:
|
||
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="用户名或密码错误")
|
||
|
|
token = create_access_token(user["id"])
|
||
|
|
return {"code": 0, "message": "ok", "data": {"token": token, "user": user}}
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/me")
|
||
|
|
def me(current_user: dict = Depends(get_current_user)) -> dict:
|
||
|
|
return {"code": 0, "message": "ok", "data": current_user}
|