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

11 months ago
  1. from fastapi import FastAPI,HTTPException
  2. from dependencies import *
  3. from internal.schemas import *
  4. # 初始化 FastAPI 应用
  5. app = FastAPI()
  6. # 数据库连接参数
  7. # 定义登录接口
  8. @app.post("/token", response_model=Token)
  9. async def login_for_access_token(username: str, password: str):
  10. user = authenticate_user(username,password)
  11. if not user:
  12. raise HTTPException(
  13. status_code=status.HTTP_401_UNAUTHORIZED,
  14. detail="Incorrect username or password",
  15. headers={"WWW-Authenticate": "Bearer"},
  16. )
  17. access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
  18. access_token = create_access_token(
  19. data={"sub": user.username}, expires_delta=access_token_expires
  20. )
  21. return Token(access_token=access_token, token_type="bearer")