From a8baf19224bb2f7f05ab1c66d4b8c944ed3e5389 Mon Sep 17 00:00:00 2001 From: huangmin <2927933426@qq.com> Date: Sat, 6 Jun 2026 15:40:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(auth):=20=E4=BC=98=E5=8C=96=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=90=8E=E9=A1=B5=E9=9D=A2=E8=B7=B3=E8=BD=AC=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 新增工具函数处理URL鉴权token清理 2. 使用location.replace替代reload避免重复提交 3. 在全局认证提供者中自动清理URL中的token参数 --- src/components/auth/GlobalAuthProvider.tsx | 2 ++ src/components/auth/LoginModal.tsx | 4 +++- src/utils/authUrlToken.ts | 25 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/utils/authUrlToken.ts diff --git a/src/components/auth/GlobalAuthProvider.tsx b/src/components/auth/GlobalAuthProvider.tsx index 2bc28a06..e6ac522b 100644 --- a/src/components/auth/GlobalAuthProvider.tsx +++ b/src/components/auth/GlobalAuthProvider.tsx @@ -5,6 +5,7 @@ import { useSearchParams } from "next/navigation"; import { LoginModal } from "@/components/auth/LoginModal"; import { getStoredLoginToken, useUserStore } from "@/store/userStore"; import { installAuthFetchInterceptor } from "@/utils/authFetch"; +import { removeAuthTokenFromCurrentUrl } from "@/utils/authUrlToken"; export function GlobalAuthProvider({ children }: { children: ReactNode }) { const searchParams = useSearchParams(); @@ -18,6 +19,7 @@ export function GlobalAuthProvider({ children }: { children: ReactNode }) { useEffect(() => { if (queryToken) { + removeAuthTokenFromCurrentUrl(); setIsResolvingQueryToken(true); void fetchUserInfo(queryToken, { forceRefresh: true }).finally(() => { setIsResolvingQueryToken(false); diff --git a/src/components/auth/LoginModal.tsx b/src/components/auth/LoginModal.tsx index e06f5edc..713242d8 100644 --- a/src/components/auth/LoginModal.tsx +++ b/src/components/auth/LoginModal.tsx @@ -10,6 +10,7 @@ import { PRIVACY_POLICY_KEY_PRIVACY_POLICY, PRIVACY_POLICY_KEY_USER_TERMS, } from "@/utils/openLatestPrivacyPolicyLink"; +import { getCurrentUrlWithoutAuthToken } from "@/utils/authUrlToken"; type LoginMode = "code" | "wechat" | "password"; type CodePurpose = "login" | "wechat-bind"; @@ -114,7 +115,8 @@ export function LoginModal() { } stopWxPoll(); closeLoginModal(); - window.location.reload(); + const newUrl = getCurrentUrlWithoutAuthToken(); + window.location.replace(newUrl); }, [closeLoginModal, fetchUserInfo, stopWxPoll, t] ); diff --git a/src/utils/authUrlToken.ts b/src/utils/authUrlToken.ts new file mode 100644 index 00000000..280a2789 --- /dev/null +++ b/src/utils/authUrlToken.ts @@ -0,0 +1,25 @@ +"use client"; + +const AUTH_TOKEN_QUERY_KEY = "token"; + +export function removeAuthTokenFromCurrentUrl(): string | null { + if (typeof window === "undefined") return null; + + const url = new URL(window.location.href); + if (!url.searchParams.has(AUTH_TOKEN_QUERY_KEY)) { + return `${url.pathname}${url.search}${url.hash}`; + } + + url.searchParams.delete(AUTH_TOKEN_QUERY_KEY); + const cleanedUrl = `${url.pathname}${url.search}${url.hash}`; + window.history.replaceState(window.history.state, "", cleanedUrl); + return cleanedUrl; +} + +export function getCurrentUrlWithoutAuthToken(): string { + if (typeof window === "undefined") return "/"; + + const url = new URL(window.location.href); + url.searchParams.delete(AUTH_TOKEN_QUERY_KEY); + return `${url.pathname}${url.search}${url.hash}` || "/"; +}