第一次提交
This commit is contained in:
30
backend/app/modules/auth/router.py
Normal file
30
backend/app/modules/auth/router.py
Normal file
@@ -0,0 +1,30 @@
|
||||
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}
|
||||
Reference in New Issue
Block a user