From edce43d45f5e55e5795bd496c59c6fe9b4b2b32f Mon Sep 17 00:00:00 2001 From: fawney19 Date: Tue, 16 Dec 2025 13:42:26 +0800 Subject: [PATCH] fix(auth): make get_current_user and get_current_user_from_header async functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 get_current_user 和 get_current_user_from_header 函数声明为 async, 并更新 AuthService.verify_token 的调用为 await,以正确处理异步 Token 验证。 --- src/utils/auth_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/auth_utils.py b/src/utils/auth_utils.py index da28a99..23670c2 100644 --- a/src/utils/auth_utils.py +++ b/src/utils/auth_utils.py @@ -19,7 +19,7 @@ from ..models.database import User, UserRole security = HTTPBearer() -def get_current_user( +async def get_current_user( credentials: HTTPAuthorizationCredentials = Depends(security), db: Session = Depends(get_db) ) -> User: """ @@ -41,7 +41,7 @@ def get_current_user( try: # 验证Token格式和签名 try: - payload = AuthService.verify_token(token) + payload = await AuthService.verify_token(token) except HTTPException as token_error: # 保持原始的HTTP状态码(如401 Unauthorized),不要转换为403 logger.error(f"Token验证失败: {token_error.status_code}: {token_error.detail}, Token前10位: {token[:10]}...") @@ -122,7 +122,7 @@ def get_current_user( ) -def get_current_user_from_header( +async def get_current_user_from_header( authorization: Optional[str] = Header(None), db: Session = Depends(get_db) ) -> User: """ @@ -144,7 +144,7 @@ def get_current_user_from_header( token = authorization.replace("Bearer ", "") try: - payload = AuthService.verify_token(token) + payload = await AuthService.verify_token(token) user_id = payload.get("user_id") if not user_id: