diff --git a/src/app/(auth)/authorize/page.tsx b/src/app/(auth)/authorize/page.tsx index f2e2a48..605d7df 100644 --- a/src/app/(auth)/authorize/page.tsx +++ b/src/app/(auth)/authorize/page.tsx @@ -1,6 +1,8 @@ "use client"; +import { useEffect } from "react"; import Link from "next/link"; +import { useRouter, useSearchParams } from "next/navigation"; import { MessageCircleCode } from "lucide-react"; import { UserAuthorize } from "@/components/auth/user-authorize"; @@ -10,6 +12,16 @@ type Props = { }; export default function AuthPage({ searchParams }: Props) { + const router = useRouter(); + const urlSearchParams = useSearchParams(); + const callbackUrl = urlSearchParams.get("callbackUrl"); + + useEffect(() => { + if (callbackUrl) { + router.replace(callbackUrl); + } + }, [callbackUrl, router]); + return (
diff --git a/src/app/(oauth)/oauth/authorize/page.tsx b/src/app/(oauth)/oauth/authorize/page.tsx index 6c42252..230534c 100644 --- a/src/app/(oauth)/oauth/authorize/page.tsx +++ b/src/app/(oauth)/oauth/authorize/page.tsx @@ -21,7 +21,17 @@ export default async function OAuthAuthorization({ // 检查用户是否已登录 const user = await getCurrentUser(); if (!user?.id) { - redirect("/sign-in"); + // 构建当前 URL 作为回调地址 + const currentUrl = new URL( + "/oauth/authorize", + process.env.NEXT_PUBLIC_HOST_URL, + ); + Object.entries(searchParams).forEach(([key, value]) => { + if (value) currentUrl.searchParams.append(key, value.toString()); + }); + redirect( + `/sign-in?callbackUrl=${encodeURIComponent(currentUrl.toString())}`, + ); } // 验证必要的参数