diff --git a/src/app/(auth)/sign-in/page.tsx b/src/app/(auth)/sign-in/page.tsx index d5c07c8..0f5d72d 100644 --- a/src/app/(auth)/sign-in/page.tsx +++ b/src/app/(auth)/sign-in/page.tsx @@ -32,7 +32,7 @@ export default function LoginPage() {
Welcome to{" "} - Q58论坛 Connect + Q58 Connect
diff --git a/src/app/(oauth)/oauth/authorize/page.tsx b/src/app/(oauth)/oauth/authorize/page.tsx index 7f9b6f7..6c42252 100644 --- a/src/app/(oauth)/oauth/authorize/page.tsx +++ b/src/app/(oauth)/oauth/authorize/page.tsx @@ -6,18 +6,19 @@ import { Authorizing } from "@/components/auth/authorizing"; import { ErrorCard } from "@/components/auth/error-card"; export interface AuthorizeParams { + scope?: string; response_type: string; client_id: string; redirect_uri: string; - scope?: string; state?: string; } -export default async function AuthorizePage({ +export default async function OAuthAuthorization({ searchParams, }: { searchParams: AuthorizeParams; }) { + // 检查用户是否已登录 const user = await getCurrentUser(); if (!user?.id) { redirect("/sign-in"); @@ -57,6 +58,7 @@ export default async function AuthorizePage({ ); } + // 验证客户端 const client = await getClientByClientId(searchParams.client_id); if (!client) { return ( @@ -102,25 +104,10 @@ export default async function AuthorizePage({ ); } - // 构建 OAuth 参数 - const oauthParams = new URLSearchParams(); - oauthParams.set("response_type", searchParams.response_type); - oauthParams.set("client_id", searchParams.client_id); - oauthParams.set("redirect_uri", searchParams.redirect_uri); - if (searchParams.scope) oauthParams.set("scope", searchParams.scope); - if (searchParams.state) oauthParams.set("state", searchParams.state); - - // 转换参数格式 - const authorizeParams = { - oauth: btoa(oauthParams.toString()), - clientId: client.id, - scope: searchParams.scope || "read_profile", - redirectUri: searchParams.redirect_uri, - }; - + // 使用原始的 Authorizing 组件处理授权流程 return ( -
- +
+
); } diff --git a/src/components/auth/authorizing.tsx b/src/components/auth/authorizing.tsx index 21b3bd4..a77f724 100644 --- a/src/components/auth/authorizing.tsx +++ b/src/components/auth/authorizing.tsx @@ -1,67 +1,60 @@ "use client"; -import { useEffect, useState } from "react"; -import { useRouter } from "next/navigation"; -import { handleAuthorizeAction } from "@/actions/authorizing"; +import { useCallback, useEffect, useState } from "react"; +import { useRouter, useSearchParams } from "next/navigation"; +import { getDiscourseSSOUrl } from "@/actions/discourse-sso-url"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { ErrorCard } from "@/components/auth/error-card"; -interface AuthorizingProps { - oauth: string; - clientId: string; - scope: string; - redirectUri: string; -} - -export function Authorizing({ - oauth, - clientId, - scope, - redirectUri, -}: AuthorizingProps) { - const [error, setError] = useState(null); +export function Authorizing() { + const router = useRouter(); + const searchParams = useSearchParams(); + const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(true); - useEffect(() => { - const authorize = async () => { - try { - const result = await handleAuthorizeAction(oauth, clientId, scope); - if (result.error) { - setError(result.error); - } else if (result.redirectUrl) { - const url = await result.redirectUrl; - window.location.href = url; - } else { - setError("授权响应无效"); - } - } catch (err) { - console.error("授权过程出错:", err); - setError(err instanceof Error ? err.message : "授权过程发生未知错误"); - } finally { - setIsLoading(false); - } - }; + const signInCallback = useCallback(async () => { + try { + const url = await getDiscourseSSOUrl(searchParams.toString()); + router.push(url); + } catch (error) { + setError(error); + setIsLoading(false); + } + }, [router, searchParams]); - authorize(); - }, [oauth, clientId, scope]); + useEffect(() => { + // Delay 3s get sso url go to ... + const timer = setTimeout(signInCallback, 3); + return () => { + clearTimeout(timer); + }; + }, [signInCallback]); if (error) { return ( -
- -
+ + +
+
+
+ + 授权失败 + +
+ +

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

+
+
); } return ( - +
diff --git a/src/components/auth/user-auth-form.tsx b/src/components/auth/user-auth-form.tsx index 0177179..c63e0d3 100644 --- a/src/components/auth/user-auth-form.tsx +++ b/src/components/auth/user-auth-form.tsx @@ -62,7 +62,7 @@ export function UserAuthForm({ ) : ( )}{" "} - Q58 + Q58论坛
);