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}` || "/"; +}