diff --git a/src/app/(oauth)/oauth/authorize/page.tsx b/src/app/(oauth)/oauth/authorize/page.tsx index 6c42252..a79ed3d 100644 --- a/src/app/(oauth)/oauth/authorize/page.tsx +++ b/src/app/(oauth)/oauth/authorize/page.tsx @@ -4,6 +4,7 @@ import { getClientByClientId } from "@/lib/dto/client"; import { getCurrentUser } from "@/lib/session"; import { Authorizing } from "@/components/auth/authorizing"; import { ErrorCard } from "@/components/auth/error-card"; +import { SaveOAuthParams } from "@/components/auth/save-oauth-params"; export interface AuthorizeParams { scope?: string; @@ -21,7 +22,9 @@ export default async function OAuthAuthorization({ // 检查用户是否已登录 const user = await getCurrentUser(); if (!user?.id) { - redirect("/sign-in"); + return ( + + ); } // 验证必要的参数 diff --git a/src/app/authorize/page.tsx b/src/app/authorize/page.tsx new file mode 100644 index 0000000..5a791b0 --- /dev/null +++ b/src/app/authorize/page.tsx @@ -0,0 +1,38 @@ +"use client"; + +import { useEffect } from "react"; +import { useRouter } from "next/navigation"; + +export default function AuthorizePage() { + const router = useRouter(); + + useEffect(() => { + // 检查是否有待处理的 OAuth 请求 + const pendingOAuth = localStorage.getItem("oauth_pending"); + if (pendingOAuth) { + try { + const params = JSON.parse(pendingOAuth); + // 清除存储的 OAuth 参数 + localStorage.removeItem("oauth_pending"); + // 重定向到 OAuth 授权页面 + const searchParams = new URLSearchParams(params); + router.push(`/oauth/authorize?${searchParams.toString()}`); + } catch (error) { + console.error("Failed to process OAuth params:", error); + router.push("/dashboard"); + } + } else { + // 如果没有待处理的 OAuth 请求,直接进入仪表板 + router.push("/dashboard"); + } + }, [router]); + + return ( +
+
+
+

正在处理...

+
+
+ ); +} diff --git a/src/components/auth/save-oauth-params.tsx b/src/components/auth/save-oauth-params.tsx new file mode 100644 index 0000000..f966dde --- /dev/null +++ b/src/components/auth/save-oauth-params.tsx @@ -0,0 +1,21 @@ +"use client"; + +import { useEffect } from "react"; +import { redirect } from "next/navigation"; + +import { AuthorizeParams } from "@/app/(oauth)/oauth/authorize/page"; + +export function SaveOAuthParams({ + searchParams, + redirectTo, +}: { + searchParams: AuthorizeParams; + redirectTo: string; +}) { + useEffect(() => { + localStorage.setItem("oauth_pending", JSON.stringify(searchParams)); + window.location.href = redirectTo; + }, [searchParams, redirectTo]); + + return null; +} diff --git a/src/components/auth/user-auth-form.tsx b/src/components/auth/user-auth-form.tsx index e86c5ad..ddbcec0 100644 --- a/src/components/auth/user-auth-form.tsx +++ b/src/components/auth/user-auth-form.tsx @@ -1,7 +1,7 @@ "use client"; import * as React from "react"; -import { useRouter, useSearchParams } from "next/navigation"; +import { useRouter } from "next/navigation"; import { Loader2, MessageCircleCode } from "lucide-react"; import { cn } from "@/lib/utils"; @@ -19,39 +19,15 @@ export function UserAuthForm({ const [isLoading, setIsLoading] = React.useState(false); const router = useRouter(); const { toast } = useToast(); - const searchParams = useSearchParams(); const signIn = () => { React.startTransition(async () => { try { - const body: Record = {}; - - // 如果存在 OAuth 参数,添加到请求体 - if (searchParams?.has("client_id")) { - const oauthParams = new URLSearchParams(); - [ - "client_id", - "redirect_uri", - "response_type", - "state", - "scope", - ].forEach((param) => { - const value = searchParams.get(param); - if (value) { - oauthParams.append(param, value); - } - }); - if (oauthParams.toString()) { - body.oauth_params = oauthParams.toString(); - } - } - const response = await fetch("/api/auth/q58", { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify(body), }); if (!response.ok || response.status !== 200) {