diff --git a/src/actions/user-authorize.ts b/src/actions/user-authorize.ts index 3382e75..7133c47 100644 --- a/src/actions/user-authorize.ts +++ b/src/actions/user-authorize.ts @@ -4,10 +4,21 @@ import { redirect } from "next/navigation"; import { signIn as nextSignIn } from "@/auth"; export async function signIn(data: Record) { - const { sso, sig } = data; + const { sso, sig, oauth } = data; + + // 先进行 SSO 登录 await nextSignIn("credentials", { sso, sig }); - // 从 sso 参数中获取 return_sso_url + // 如果有 OAuth 参数,重定向到 OAuth 授权页面 + if (oauth) { + const oauthParams = new URLSearchParams(atob(oauth)); + if (oauthParams.has("client_id")) { + const authUrl = `/oauth/authorize?${oauthParams.toString()}`; + redirect(authUrl); + } + } + + // 如果没有 OAuth 参数,从 SSO 参数中获取 return_sso_url const params = new URLSearchParams(atob(sso)); const returnUrl = params.get("return_sso_url"); diff --git a/src/components/auth/user-authorize.tsx b/src/components/auth/user-authorize.tsx index 0d48be7..376489c 100644 --- a/src/components/auth/user-authorize.tsx +++ b/src/components/auth/user-authorize.tsx @@ -23,58 +23,78 @@ export function UserAuthorize({ } setIsLoading(true); try { - await signIn({ ...data, redirectTo: "/dashboard" }); + // 从 URL 中获取 sso 和 sig 参数 + const url = new URL(window.location.href); + const sso = url.searchParams.get("sso"); + const sig = url.searchParams.get("sig"); + + if (!sso || !sig) { + throw new Error("缺少必要的认证参数"); + } + + // 获取原始的 OAuth 参数 + const originalOAuthParams = new URLSearchParams(window.location.search); + const oauthData: Record = {}; + Array.from(originalOAuthParams.entries()) + .filter(([key]) => key !== "sso" && key !== "sig") + .forEach(([key, value]) => { + oauthData[key] = value; + }); + + // 传递 SSO 参数和原始的 OAuth 参数 + await signIn({ + sso, + sig, + oauth: btoa(originalOAuthParams.toString()), + }); + setIsLoading(false); } catch (error) { + console.error("登录过程出错:", error); setError(error); setIsLoading(false); } - }, []); + }, [isLoading]); useEffect(() => { - const timer = setTimeout(signInCallback, 5); - return () => { - clearTimeout(timer); - }; - }, []); + signInCallback(); + }, [signInCallback]); + + if (error) { + return ( + + +
+
+
+ + 授权失败 + +
+ +

+ {error instanceof Error + ? error.message + : "登录异常,授权失败!请稍后重试。"} +

+
+
+ ); + } return ( - <> - {error ? ( - - -
-
-
- - 授权失败 - -
- -

- {error instanceof Error - ? error.message - : "登录异常,授权失败!请稍后重试。"} -

-
-
- ) : ( - - -
-
-
- - 正在处理授权 - -
- -

- {isLoading ? "请稍候,我们正在处理您的授权请求" : "正在跳转..."} -

-
-
- )} - + + +
+
+
+ 正在处理授权 +
+ +

+ {isLoading ? "请稍候,我们正在处理您的授权请求" : "正在跳转..."} +

+
+
); }