优化部分代码实现

This commit is contained in:
2025-08-28 02:23:26 +08:00
parent 9f93f6040b
commit f3a5ae9c40
5 changed files with 63 additions and 87 deletions

View File

@@ -51,48 +51,6 @@ async def router_callback_github(
code: str = Query(description="The token received from GitHub for authentication.")) -> PlainTextResponse:
"""
GitHub OAuth 回调处理
- Github 成功响应:
- JWT: {"access_token": "gho_xxxxxxxx", "token_type": "bearer", "scope": ""}
- User Info:{
"code": "grfessg1312432313421fdgs",
"user_data": {
"login": "Yuerchu",
"id": 114514,
"node_id": "xxxxx",
"avatar_url": "https://avatars.githubusercontent.com/u/114514?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Yuerchu",
"html_url": "https://github.com/Yuerchu",
"followers_url": "https://api.github.com/users/Yuerchu/followers",
"following_url": "https://api.github.com/users/Yuerchu/following{/other_user}",
"gists_url": "https://api.github.com/users/Yuerchu/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Yuerchu/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Yuerchu/subscriptions",
"organizations_url": "https://api.github.com/users/Yuerchu/orgs",
"repos_url": "https://api.github.com/users/Yuerchu/repos",
"events_url": "https://api.github.com/users/Yuerchu/events{/privacy}",
"received_events_url": "https://api.github.com/users/Yuerchu/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false,
"name": "于小丘",
"company": null,
"blog": "https://www.yxqi.cn",
"location": "ChangSha, HuNan, China",
"email": "admin@yuxiaoqiu.cn",
"hireable": null,
"bio": null,
"twitter_username": null,
"notification_email": "admin@yuxiaoqiu.cn",
"public_repos": 17,
"public_gists": 0,
"followers": 8,
"following": 8,
"created_at": "2019-04-13T11:17:33Z",
"updated_at": "2025-08-20T03:03:16Z"
}
}
- 错误响应示例:
- {
'error': 'bad_verification_code',

View File

@@ -30,18 +30,21 @@ async def router_user_session(
username = form_data.username
password = form_data.password
user = await service.user.Login(username=username, password=password)
is_login, detail = await service.user.Login(username=username, password=password)
if user is None:
raise HTTPException(status_code=400, detail="Invalid username or password")
elif user == 1:
raise HTTPException(status_code=400, detail="User account is not fully registered")
elif user == 2:
raise HTTPException(status_code=403, detail="User account is banned")
elif isinstance(user, TokenModel):
return user
if not is_login:
if detail in ["User not found", "Incorrect password"]:
raise HTTPException(status_code=400, detail="Invalid username or password")
elif detail == "Need to complete registration":
raise HTTPException(status_code=400, detail="User account is not fully registered")
elif detail == "Account is banned":
raise HTTPException(status_code=403, detail="User account is banned")
else:
raise HTTPException(status_code=500, detail="Internal server error during login")
if isinstance(detail, TokenModel):
return detail
else:
log.error(f"Unexpected return type from login service: {type(user)}")
log.error(f"Unexpected return type from login service: {type(detail)}")
raise HTTPException(status_code=500, detail="Internal server error during login")
@user_router.post(
@@ -73,13 +76,13 @@ def router_user_2fa() -> ResponseModel:
pass
@user_router.post(
path='/reset',
summary='发送密码重设邮件',
description='Send a password reset email.',
path='/code',
summary='发送验证码邮件',
description='Send a verification code email.',
)
def router_user_reset() -> ResponseModel:
def router_user_email_code() -> ResponseModel:
"""
Send a password reset email.
Send a pas
Returns:
dict: A dictionary containing information about the password reset email.
@@ -118,27 +121,6 @@ def router_user_qq() -> ResponseModel:
"""
pass
@deprecated(
version="0.0.1",
reason="邮件中带链接的激活易使得被收件服务器误判为垃圾邮件,新版更换为验证码方式"
)
@user_router.get(
path='/activate/{id}',
summary='邮件激活',
description='Activate user account via email link.',
)
def router_user_activate(id: str) -> ResponseModel:
"""
Activate user account via email link.
Args:
id (str): The activation ID from the email link.
Returns:
dict: A dictionary containing activation information.
"""
pass
@user_router.get(
path='authn/{username}',
summary='WebAuthn登录初始化',