diff --git a/app/(auth)/login/page.tsx b/app/(auth)/login/page.tsx index 8e98180aa..78b39207c 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 @@ -102,11 +118,12 @@ export default function Page() { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", + "X-Auth-Return-Redirect": "1", }, body: new URLSearchParams({ csrfToken, token, - callbackUrl: "/", + callbackUrl, }), credentials: "include", // 重要:确保 cookie 被发送和接收 }); @@ -134,12 +151,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 +191,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 +315,7 @@ export default function Page() { return; } - await signIn("google", { callbackUrl: "/" }); + await signIn("google", { redirectTo: getSafeLoginCallbackUrl() }); }; // 检查 URL 中是否有钉钉 Token(页面加载时自动登录) @@ -365,13 +382,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 +411,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(); diff --git a/public/dingtalk-callback.html b/public/dingtalk-callback.html index 8e6d92ff8..6d7319ef2 100644 --- a/public/dingtalk-callback.html +++ b/public/dingtalk-callback.html @@ -143,7 +143,7 @@
AI

正在登录...

-

正在通过钉钉确认企业身份,完成后会自动回到工作台。

+

正在通过钉钉确认企业身份,完成后会自动返回原页面。

安全连接已建立 @@ -215,7 +215,7 @@ if (typeof callbackUrl !== 'string' || !callbackUrl) return '/'; try { const parsed = new URL(callbackUrl, window.location.origin); - if (parsed.origin !== window.location.origin) return '/'; + if (parsed.origin !== window.location.origin || parsed.pathname.startsWith('//')) return '/'; return parsed.pathname + parsed.search + parsed.hash; } catch (error) { return '/'; @@ -238,6 +238,7 @@ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', + 'X-Auth-Return-Redirect': '1', }, body: new URLSearchParams({ csrfToken: csrfToken, @@ -251,7 +252,7 @@ if (loginRes.ok || loginRes.redirected) { statusText.textContent = '登录成功,正在跳转...'; - console.log('[DingTalk Callback] Login successful, redirecting to home...'); + console.log('[DingTalk Callback] Login successful, validating session...'); // 等待更长时间确保 cookie 被设置并同步 await new Promise(resolve => setTimeout(resolve, 800));