feat: Enhance Discourse SSO authentication with dynamic return URL handling

- Update signIn action to extract and use return_sso_url from SSO parameters
- Modify UserAuthorize component to dynamically retrieve SSO and signature parameters
- Improve error handling and logging in authentication process
- Update README to reflect current project deployment and simplify description
This commit is contained in:
wood chen 2025-02-21 15:30:25 +08:00
parent 8a7997f5b9
commit 1083416b17
3 changed files with 34 additions and 20 deletions

View File

@ -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.
## 许可证

View File

@ -4,7 +4,16 @@ import { redirect } from "next/navigation";
import { signIn as nextSignIn } from "@/auth";
export async function signIn(data: Record<string, any>) {
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);
}
}

View File

@ -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({
</CardHeader>
<CardContent>
<p className="text-center text-gray-500">
{error instanceof Error
? error.message
: "登录异常,授权失败!请稍后重试。"}
</p>
</CardContent>
</Card>