DocHub
Auth callback was redirecting to localhost:3000 after OAuth due to reverse proxy not preserving Host header

Google OAuth Localhost Redirect Fix

Problem

Google sign-in on production redirected users to localhost:3000 after completing the OAuth flow with Google.

Root Cause

The auth callback route at src/app/auth/callback/route.ts used:

const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || new URL(request.url).origin;

NEXT_PUBLIC_BASE_URL was not set. On the production server, Next.js runs on localhost:3000 behind a reverse proxy that doesn’t forward the Host header. So new URL(request.url).origin resolved to http://localhost:3000, and the auth callback redirected there after exchanging the OAuth code.

Fix

Hardcoded the production URL as fallback:

const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://ricoya.ipnoelp.com";

Same pattern already used by payment callback routes (with https://ricoya.net).

Sign Out Fix

Separate issue — sign out was getting stuck on a loading screen. The logout() function had setLoading(true/false) which caused the login page to render a spinner before the signOut completed. Fixed by removing loading state from logout and using await logout(); window.location.href = "/login"; in the sidebar.

Files Modified

  • src/app/auth/callback/route.ts — hardcoded production URL fallback
  • src/contexts/AuthContext.tsx — simplified logout function
  • src/components/Sidebar.tsx — await logout before redirect