diff --git a/services/hackbot-ui/app/login/page.tsx b/services/hackbot-ui/app/login/page.tsx index 1a526a5a14..1a8d905db2 100644 --- a/services/hackbot-ui/app/login/page.tsx +++ b/services/hackbot-ui/app/login/page.tsx @@ -1,21 +1,35 @@ "use client"; -import { useState } from "react"; +import { use, useState } from "react"; import { signIn } from "@/lib/auth-client"; -export default function LoginPage() { +type LoginPageProps = { + searchParams: Promise<{ + callbackURL?: string | string[]; + error?: string | string[]; + }>; +}; + +export default function LoginPage({ searchParams }: LoginPageProps) { + const params = use(searchParams); + const callbackURL = + typeof params.callbackURL === "string" ? params.callbackURL : "/"; const [loading, setLoading] = useState(false); const [error, setError] = useState(null); async function onGoogle() { setError(null); setLoading(true); + + // Keep the target across a denied sign-in so a retry still lands on it. + const errorParams = new URLSearchParams({ error: "denied", callbackURL }); + try { await signIn.social({ provider: "google", - callbackURL: "/", - errorCallbackURL: "/login?error=denied", + callbackURL, + errorCallbackURL: `/login?${errorParams.toString()}`, }); } catch (err) { setError((err as Error).message); @@ -23,9 +37,7 @@ export default function LoginPage() { } } - const denied = - typeof window !== "undefined" && - new URLSearchParams(window.location.search).get("error"); + const denied = params.error === "denied"; return (
diff --git a/services/hackbot-ui/middleware.ts b/services/hackbot-ui/middleware.ts index 11cfecc912..3310836f9f 100644 --- a/services/hackbot-ui/middleware.ts +++ b/services/hackbot-ui/middleware.ts @@ -16,7 +16,9 @@ export function middleware(req: NextRequest) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } + // Remember the requested URL so the login page can send the user back here const loginUrl = new URL("/login", req.url); + loginUrl.searchParams.set("callbackURL", req.nextUrl.toString()); return NextResponse.redirect(loginUrl); }