diff --git a/src/actions/user-authorize.ts b/src/actions/user-authorize.ts index 7133c47..c371f12 100644 --- a/src/actions/user-authorize.ts +++ b/src/actions/user-authorize.ts @@ -4,27 +4,26 @@ import { redirect } from "next/navigation"; import { signIn as nextSignIn } from "@/auth"; export async function signIn(data: Record) { - const { sso, sig, oauth } = data; + const { sso, sig, returnTo } = data; // 先进行 SSO 登录 await nextSignIn("credentials", { sso, sig }); - // 如果有 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 + // 从 sso 参数中获取 return_sso_url const params = new URLSearchParams(atob(sso)); - const returnUrl = params.get("return_sso_url"); + const returnSsoUrl = params.get("return_sso_url"); - if (!returnUrl) { + if (!returnSsoUrl) { + // 如果没有 return_sso_url,检查是否有 OAuth 参数 + const searchParams = new URLSearchParams(returnTo); + if (searchParams.has("client_id")) { + // 如果是 OAuth 流程,重定向到授权页面 + redirect(`/oauth/authorize${returnTo}`); + } + // 如果都没有,重定向到仪表板 redirect("/dashboard"); - } else { - redirect(returnUrl); } + + // 如果有 return_sso_url,重定向回 SSO 提供者 + redirect(returnSsoUrl); } diff --git a/src/components/auth/user-authorize.tsx b/src/components/auth/user-authorize.tsx index 376489c..d6ec043 100644 --- a/src/components/auth/user-authorize.tsx +++ b/src/components/auth/user-authorize.tsx @@ -32,20 +32,11 @@ export function UserAuthorize({ 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 参数 + // 传递 SSO 参数和原始的 search 参数 await signIn({ sso, sig, - oauth: btoa(originalOAuthParams.toString()), + returnTo: url.search, // 保存完整的查询参数 }); setIsLoading(false);