diff --git a/src/components/auth/user-auth-form.tsx b/src/components/auth/user-auth-form.tsx index 75af5b6..20b0342 100644 --- a/src/components/auth/user-auth-form.tsx +++ b/src/components/auth/user-auth-form.tsx @@ -31,11 +31,20 @@ export function UserAuthForm({ const signIn = () => { React.startTransition(async () => { try { + setIsLoading(true); // 构建请求体,包含 OAuth 参数 const body: Record = {}; - const savedParams = localStorage.getItem("oauth_params"); - if (savedParams) { - body.oauth_params = savedParams; + // 优先使用 URL 中的参数 + if (searchParams?.toString()) { + body.oauth_params = searchParams.toString(); + } else { + // 如果 URL 中没有参数,尝试从 localStorage 获取 + const savedParams = localStorage.getItem("oauth_params"); + if (savedParams) { + body.oauth_params = savedParams; + // 使用后清除存储的参数 + localStorage.removeItem("oauth_params"); + } } const response = await fetch("/api/auth/q58", { diff --git a/src/components/auth/user-authorize.tsx b/src/components/auth/user-authorize.tsx index d6ec043..1c131df 100644 --- a/src/components/auth/user-authorize.tsx +++ b/src/components/auth/user-authorize.tsx @@ -1,6 +1,7 @@ "use client"; import { useCallback, useEffect, useState } from "react"; +import { useRouter } from "next/navigation"; import { signIn } from "@/actions/user-authorize"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; @@ -16,6 +17,7 @@ export function UserAuthorize({ }: UserAuthorizeProps) { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); + const router = useRouter(); const signInCallback = useCallback(async () => { if (isLoading) { @@ -32,20 +34,21 @@ export function UserAuthorize({ throw new Error("缺少必要的认证参数"); } - // 传递 SSO 参数和原始的 search 参数 + // 传递 SSO 参数 await signIn({ sso, sig, - returnTo: url.search, // 保存完整的查询参数 }); + // 登录成功后刷新路由状态 + router.refresh(); setIsLoading(false); } catch (error) { console.error("登录过程出错:", error); setError(error); setIsLoading(false); } - }, [isLoading]); + }, [isLoading, router]); useEffect(() => { signInCallback();