diff --git a/app/(auth)/login/page.tsx b/app/(auth)/login/page.tsx index 8e98180aa..baad795fc 100644 --- a/app/(auth)/login/page.tsx +++ b/app/(auth)/login/page.tsx @@ -23,6 +23,21 @@ import { AntigravityBackground } from "@/components/antigravity-background"; import { createClientLogger } from "@/lib/observability/client-logger"; const logger = createClientLogger("auth-login"); + +function getSafeLoginCallbackUrl() { + const { origin, search } = window.location; + const callbackUrl = new URLSearchParams(search).get("callbackUrl"); + if (!callbackUrl?.startsWith("/")) return "/"; + + try { + const { origin: targetOrigin, pathname, search, hash } = new URL(callbackUrl, origin); + if (targetOrigin !== origin || pathname.startsWith("//")) return "/"; + return pathname + search + hash; + } catch { + return "/"; + } +} + export default function Page() { const [email, setEmail] = useState(""); const [isSuccessful, setIsSuccessful] = useState(false); @@ -70,7 +85,7 @@ export default function Page() { .then((data) => { if (cancelled) return; if (data?.valid === true && data?.userType !== "guest") { - window.location.replace("/"); + window.location.replace(getSafeLoginCallbackUrl()); return; } setIsCheckingExistingSession(false); @@ -90,6 +105,7 @@ export default function Page() { const completeDingTalkLogin = useCallback(async (token: string) => { logger.debug("[DingTalk] Completing login with token", { hasToken: Boolean(token) }); setIsHandlingToken(true); + const callbackUrl = getSafeLoginCallbackUrl(); try { // 获取 CSRF token @@ -106,7 +122,7 @@ export default function Page() { body: new URLSearchParams({ csrfToken, token, - callbackUrl: "/", + callbackUrl, }), credentials: "include", // 重要:确保 cookie 被发送和接收 }); @@ -134,12 +150,12 @@ export default function Page() { }); if (session && session.user) { - logger.debug("[DingTalk] Session validated, redirecting to home..."); + logger.debug("[DingTalk] Session validated, redirecting..."); setIsSuccessful(true); // 使用 replace 而不是 href,避免在历史记录中留下登录页 // 增加短暂延迟确保 cookie 完全同步 setTimeout(() => { - window.location.replace("/"); + window.location.replace(callbackUrl); }, 200); } else { logger.error("[DingTalk] Session not created after login"); @@ -174,7 +190,7 @@ export default function Page() { // 使用 replace 而不是 href,避免在历史记录中留下登录页 // 增加短暂延迟确保 cookie 完全同步 setTimeout(() => { - window.location.replace(result.url || "/"); + window.location.replace(callbackUrl); }, 200); } else { logger.error("[DingTalk] Session not created"); @@ -298,7 +314,7 @@ export default function Page() { return; } - await signIn("google", { callbackUrl: "/" }); + await signIn("google", { redirectTo: getSafeLoginCallbackUrl() }); }; // 检查 URL 中是否有钉钉 Token(页面加载时自动登录) @@ -365,13 +381,14 @@ export default function Page() { description: "提交的信息校验失败", }); } else if (state.status === "success") { - toast({ type: "success", description: "登录成功,正在进入首页" }); + const callbackUrl = getSafeLoginCallbackUrl(); + toast({ type: "success", description: "登录成功,正在跳转" }); setIsSuccessful(true); loginRedirectTimerRef.current = window.setTimeout(() => { - window.location.replace("/"); + window.location.replace(callbackUrl); }, 120); loginRedirectFallbackTimerRef.current = window.setTimeout(() => { - window.location.assign("/"); + window.location.assign(callbackUrl); }, 1600); } }, [state.status]); @@ -393,23 +410,6 @@ export default function Page() { }; }, []); - const getSafeLoginCallbackUrl = () => { - if (typeof window === "undefined") return "/"; - - const callbackUrl = new URLSearchParams(window.location.search).get( - "callbackUrl", - ); - if ( - callbackUrl && - callbackUrl.startsWith("/") && - !callbackUrl.startsWith("//") - ) { - return callbackUrl; - } - - return "/"; - }; - const buildDingTalkLoginUrl = (orgName?: string) => { const params = new URLSearchParams(); if (orgName) params.set("org", orgName); diff --git a/app/feedback/[id]/page.tsx b/app/feedback/[id]/page.tsx index 3839cb1fd..9f908425e 100644 --- a/app/feedback/[id]/page.tsx +++ b/app/feedback/[id]/page.tsx @@ -7,10 +7,13 @@ import { formatFeedbackTime } from "@/lib/canvas/feedback-contract"; export const dynamic = "force-dynamic"; export default async function CanvasFeedbackPage({ params }: { params: Promise<{ id: string }> }) { - const session = await auth(); - if (!session?.user?.id) redirect("/login"); - const id = (await params).id; + const { id } = await params; if (!z.string().uuid().safeParse(id).success) notFound(); + const session = await auth(); + if (!session?.user?.id || session.user.type === "guest") { + const query = new URLSearchParams({ callbackUrl: `/feedback/${id}` }); + redirect(`/login?${query}`); + } const feedback = await findCanvasFeedback(id); if (!feedback || (feedback.userId !== session.user.id && session.user.type !== "admin")) notFound();