From 4e49632e23d216d0176c4137d21204b5a6f23d3b Mon Sep 17 00:00:00 2001 From: Chris Farhood Date: Sun, 5 Jul 2026 22:39:13 -0400 Subject: [PATCH] auth: login page resumes the OAuth authorize flow after sign-in Better Auth redirects to the login page with the original signed authorize params; hand them back to /oauth2/authorize as the post-sign-in callback so a code is issued (was defaulting to '/', producing a blank page). --- src/server.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/server.ts b/src/server.ts index 3b90e45..58909a9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -12,9 +12,12 @@ import { auth } from "./auth.js"; const PORT = Number(process.env.PORT ?? 8080); const handler = toNodeHandler(auth); -function loginPage(params: URLSearchParams): string { - // Preserve wherever the OAuth flow wants to return to after login. - const cb = params.get("callbackURL") || params.get("redirect") || "/"; +function loginPage(rawSearch: string): string { + // Better Auth's oauth-provider redirects here with the original, signed + // authorize params in the query. After sign-in we must hand them back to the + // authorize endpoint verbatim so it resumes and issues the code. If there's no + // OAuth context (a stray visit), fall back to the portal. + const cb = rawSearch ? "/api/auth/oauth2/authorize" + rawSearch : "/portal"; const social = (provider: string, label: string) => ` `; return ` @@ -58,7 +61,7 @@ const server = createServer((req, res) => { } if (url.pathname === "/login" && req.method === "GET") { res.writeHead(200, { "content-type": "text/html; charset=utf-8" }); - res.end(loginPage(url.searchParams)); + res.end(loginPage(url.search)); return; } // Everything else -> Better Auth (async handler).