This commit is contained in:
27942
2026-02-25 20:12:26 +08:00
parent a89d2d58d9
commit 34c41c52f8
2 changed files with 27 additions and 0 deletions

View File

@@ -41,3 +41,29 @@ def login(request):
)
return Response({"token": token})
@api_view(["GET"])
def get_info(request):
"""
获取当前登录账号的详细信息:
- 从 Authorization 头解析 token
- 查 AuthToken 表反查用户名
- 返回用户名、登录时间等
"""
auth_header = request.headers.get("Authorization", "")
token = auth_header.replace("Bearer ", "").strip() if auth_header else ""
if not token:
return Response({"detail": "未提供认证令牌"}, status=status.HTTP_401_UNAUTHORIZED)
try:
record = AuthToken.objects.get(token=token)
except AuthToken.DoesNotExist:
return Response({"detail": "令牌无效或已过期"}, status=status.HTTP_401_UNAUTHORIZED)
return Response({
"username": record.username,
"token": record.token,
"created_at": record.created_at,
})

View File

@@ -12,6 +12,7 @@ urlpatterns = [
# ─── 认证 ───
path("api/auth/login", auth.login),
path("api/auth/get_info", auth.get_info),
# ─── Worker ───
path("api/workers", workers.worker_list),