fix: Google OAuth 로그인 시 localhost:3000 리다이렉트 문제 수정

- app/auth/callback/route.ts: NEXT_PUBLIC_SITE_URL 우선, x-forwarded-host 폴백
  Vercel 내부 라우팅에서 request.url의 origin이 localhost로 잡히는 경우 대응
- app/login/page.tsx: signInWithOAuth redirectTo를 NEXT_PUBLIC_SITE_URL 기준으로 변경
  window.location.origin 대신 명시적 환경변수 사용으로 안정성 확보

Vercel 환경변수에 NEXT_PUBLIC_SITE_URL=https://<도메인> 추가 필요

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-19 23:44:54 +09:00
parent 1193a075c2
commit 0222eca381
2 changed files with 15 additions and 11 deletions

View File

@@ -6,21 +6,23 @@ export async function GET(request: Request) {
const code = searchParams.get('code'); const code = searchParams.get('code');
const next = searchParams.get('next') ?? '/mypage'; const next = searchParams.get('next') ?? '/mypage';
// 프로덕션 기준 URL 결정
// 우선순위: NEXT_PUBLIC_SITE_URL > x-forwarded-host > origin
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL;
const forwardedHost = request.headers.get('x-forwarded-host');
const baseUrl =
siteUrl ??
(process.env.NODE_ENV !== 'development' && forwardedHost
? `https://${forwardedHost}`
: origin);
if (code) { if (code) {
const supabase = await createClient(); const supabase = await createClient();
const { error } = await supabase.auth.exchangeCodeForSession(code); const { error } = await supabase.auth.exchangeCodeForSession(code);
if (!error) { if (!error) {
const forwardedHost = request.headers.get('x-forwarded-host'); return NextResponse.redirect(`${baseUrl}${next}`);
const isLocalEnv = process.env.NODE_ENV === 'development';
if (isLocalEnv) {
return NextResponse.redirect(`${origin}${next}`);
} else if (forwardedHost) {
return NextResponse.redirect(`https://${forwardedHost}${next}`);
} else {
return NextResponse.redirect(`${origin}${next}`);
}
} }
} }
return NextResponse.redirect(`${origin}/login?error=auth-callback-error`); return NextResponse.redirect(`${baseUrl}/login?error=auth-callback-error`);
} }

View File

@@ -60,9 +60,11 @@ function LoginForm() {
}; };
const handleGoogleLogin = async () => { const handleGoogleLogin = async () => {
// NEXT_PUBLIC_SITE_URL 이 설정되어 있으면 우선 사용 (localhost 리다이렉트 방지)
const base = process.env.NEXT_PUBLIC_SITE_URL ?? window.location.origin;
const { error } = await supabase.auth.signInWithOAuth({ const { error } = await supabase.auth.signInWithOAuth({
provider: 'google', provider: 'google',
options: { redirectTo: `${window.location.origin}/auth/callback` }, options: { redirectTo: `${base}/auth/callback` },
}); });
if (error) setMessage('Google 로그인 오류: ' + error.message); if (error) setMessage('Google 로그인 오류: ' + error.message);
}; };