From 0d1228ec303febf7a3488735a4c6851dc3695b3c Mon Sep 17 00:00:00 2001 From: wood chen Date: Fri, 21 Feb 2025 18:00:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20OAuth=20=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E6=B5=81=E7=A8=8B=EF=BC=8C=E6=94=B9=E8=BF=9B=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E8=8E=B7=E5=8F=96=E5=92=8C=E5=8A=A0=E8=BD=BD=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/auth/user-auth-form.tsx | 15 ++++++++++++--- src/components/auth/user-authorize.tsx | 9 ++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) 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();