diff --git a/README.md b/README.md index 2e9d2d5..61fbf25 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Discourse Connect -这是一个基于 [Next.js](https://nextjs.org/) 的项目,实现了使用 Discourse SSO (Single Sign-On) 用户系统的 OAuth 认证功能。 +这是一个基于Next.js,实现了使用 Discourse SSO (Single Sign-On) 用户系统的 OAuth 认证功能。 ## 项目概述 @@ -12,19 +12,7 @@ - OAuth 2.0 协议支持 - 使用 Next.js 框架构建,提供良好的性能和开发体验 -## 开始使用 - -首先,运行开发服务器: - -```bash -pnpm dev -# 或 -pnpm turbo -``` - -在浏览器中打开 [http://localhost:3000](http://localhost:3000) 查看结果。 - -您可以通过修改 `app/page.tsx` 来开始编辑页面。当您编辑文件时,页面会自动更新。 +本项目部署在vercel, 数据库使用Neon. ## 许可证 diff --git a/src/actions/user-authorize.ts b/src/actions/user-authorize.ts index 4bc5874..3382e75 100644 --- a/src/actions/user-authorize.ts +++ b/src/actions/user-authorize.ts @@ -4,7 +4,16 @@ import { redirect } from "next/navigation"; import { signIn as nextSignIn } from "@/auth"; export async function signIn(data: Record) { - const { redirectTo, ...credentials } = data; - await nextSignIn("credentials", credentials); - redirect(redirectTo || "/dashboard"); + const { sso, sig } = data; + await nextSignIn("credentials", { sso, sig }); + + // 从 sso 参数中获取 return_sso_url + const params = new URLSearchParams(atob(sso)); + const returnUrl = params.get("return_sso_url"); + + if (!returnUrl) { + redirect("/dashboard"); + } else { + redirect(returnUrl); + } } diff --git a/src/components/auth/user-authorize.tsx b/src/components/auth/user-authorize.tsx index 8c7bc59..ca06d09 100644 --- a/src/components/auth/user-authorize.tsx +++ b/src/components/auth/user-authorize.tsx @@ -27,17 +27,32 @@ 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"); + + console.log("准备登录,参数:", { + sso: sso?.substring(0, 20) + "...", + sig: sig?.substring(0, 20) + "...", + }); + + if (!sso || !sig) { + throw new Error("缺少必要的认证参数"); + } + + await signIn({ sso, sig }); // 更新 session await update(); // 强制刷新页面状态 router.refresh(); setIsLoading(false); } catch (error) { + console.error("登录过程出错:", error); setError(error); setIsLoading(false); } - }, [data, isLoading, update, router]); + }, [isLoading, update, router]); useEffect(() => { // 直接执行登录回调 @@ -57,7 +72,9 @@ export function UserAuthorize({

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