Make Google OAuth redirect_uri dynamic based on request origin to support both shahikitchen.se and www.shahikitchen.se

This commit is contained in:
root
2026-07-03 03:06:44 +00:00
parent 8ccf033329
commit 8120a09a0c
3 changed files with 47 additions and 18 deletions
+12 -3
View File
@@ -16,7 +16,7 @@ const OAUTH_STATE_COOKIE = 'shahi_google_oauth_state';
export async function GET(request: Request) {
if (!isGoogleOAuthConfigured()) {
return NextResponse.redirect(new URL('/login?tab=customer&error=google_not_configured', getOrigin()));
return NextResponse.redirect(new URL('/login?tab=customer&error=google_not_configured', getOrigin(request)));
}
const url = new URL(request.url);
@@ -39,9 +39,18 @@ export async function GET(request: Request) {
maxAge: 600,
});
return NextResponse.redirect(buildGoogleAuthUrl(state));
return NextResponse.redirect(buildGoogleAuthUrl(state, request));
}
function getOrigin(): string {
function getOrigin(req?: Request): string {
if (req) {
try {
const url = new URL(req.url);
if (process.env.NODE_ENV === 'production' && url.protocol === 'http:') {
url.protocol = 'https:';
}
return url.origin;
} catch {}
}
return process.env.NEXT_PUBLIC_SITE_URL ?? 'http://localhost:3000';
}