You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

24 lines
817 B

from fastapi import FastAPI,HTTPException
from dependencies import *
from internal.schemas import *
# 初始化 FastAPI 应用
app = FastAPI()
# 数据库连接参数
# 定义登录接口
@app.post("/token", response_model=Token)
async def login_for_access_token(username: str, password: str):
user = authenticate_user(username,password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(
data={"sub": user.username}, expires_delta=access_token_expires
)
return Token(access_token=access_token, token_type="bearer")