初步开发完成
This commit is contained in:
Binary file not shown.
+7
-19
@@ -17,25 +17,13 @@ def login(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depend
|
||||
"""用户登录"""
|
||||
user = db.query(User).filter(User.username == form_data.username).first()
|
||||
|
||||
# 检查是否为测试环境
|
||||
is_test = os.environ.get("TESTING", "False").lower() == "true"
|
||||
|
||||
if is_test:
|
||||
# 测试环境:检查用户名和密码
|
||||
if not user or form_data.password != "test_password":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
else:
|
||||
# 生产环境:正常验证密码
|
||||
if not user or not verify_password(form_data.password, user.password):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
# 密码验证:比较用户存储的密码
|
||||
if not user or form_data.password != user.password:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
access_token_expires = timedelta(minutes=settings.access_token_expire_minutes)
|
||||
access_token = create_access_token(
|
||||
|
||||
Reference in New Issue
Block a user