2026-02-03 10:51:22 +09:00
|
|
|
import { createClient } from "@/utils/supabase/server";
|
|
|
|
|
import { NextResponse } from "next/server";
|
2026-02-04 09:35:42 +09:00
|
|
|
import { AUTH_ERROR_MESSAGES, AUTH_ROUTES } from "@/features/auth/constants";
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [인증 콜백 라우트 핸들러]
|
|
|
|
|
*
|
2026-02-04 09:35:42 +09:00
|
|
|
* Supabase 인증 이메일(회원가입 확인, 비밀번호 재설정 등) 및 OAuth(소셜 로그인)
|
2026-02-03 10:51:22 +09:00
|
|
|
* 리다이렉트될 때 호출되는 API 라우트입니다.
|
|
|
|
|
*/
|
|
|
|
|
export async function GET(request: Request) {
|
|
|
|
|
const { searchParams, origin } = new URL(request.url);
|
|
|
|
|
|
2026-02-04 09:35:42 +09:00
|
|
|
// 1. URL에서 주요 직접 파라미터 및 에러 추출
|
2026-02-03 10:51:22 +09:00
|
|
|
const code = searchParams.get("code");
|
2026-02-04 09:35:42 +09:00
|
|
|
const next = searchParams.get("next") ?? AUTH_ROUTES.HOME;
|
|
|
|
|
const error = searchParams.get("error");
|
|
|
|
|
const error_code = searchParams.get("error_code");
|
|
|
|
|
const error_description = searchParams.get("error_description");
|
|
|
|
|
|
|
|
|
|
// 2. 인증 오류가 있는 경우 (예: 구글 로그인 취소 등)
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error("Auth callback error parameter:", {
|
|
|
|
|
error,
|
|
|
|
|
error_code,
|
|
|
|
|
error_description,
|
|
|
|
|
});
|
2026-02-03 10:51:22 +09:00
|
|
|
|
2026-02-04 09:35:42 +09:00
|
|
|
let message = AUTH_ERROR_MESSAGES.DEFAULT;
|
|
|
|
|
|
|
|
|
|
// 에러 종류에 따른 메시지 분기
|
|
|
|
|
if (error === "access_denied") {
|
|
|
|
|
message = AUTH_ERROR_MESSAGES.OAUTH_ACCESS_DENIED;
|
|
|
|
|
} else if (error === "server_error") {
|
|
|
|
|
message = AUTH_ERROR_MESSAGES.OAUTH_SERVER_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
`${origin}${AUTH_ROUTES.LOGIN}?message=${encodeURIComponent(message)}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. code가 있으면 세션으로 교환 (정상 플로우)
|
2026-02-03 10:51:22 +09:00
|
|
|
if (code) {
|
|
|
|
|
const supabase = await createClient();
|
2026-02-04 09:35:42 +09:00
|
|
|
const { error: exchangeError } =
|
|
|
|
|
await supabase.auth.exchangeCodeForSession(code);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
2026-02-04 09:35:42 +09:00
|
|
|
if (!exchangeError) {
|
|
|
|
|
// 세션 교환 성공 - 원래 목적지로 리다이렉트
|
|
|
|
|
const forwardedHost = request.headers.get("x-forwarded-host");
|
2026-02-03 10:51:22 +09:00
|
|
|
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}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-04 09:35:42 +09:00
|
|
|
// 세션 교환 실패 시 로그 및 에러 메시지 설정
|
|
|
|
|
console.error("Auth exchange error:", exchangeError.message);
|
2026-02-03 10:51:22 +09:00
|
|
|
}
|
|
|
|
|
|
2026-02-04 09:35:42 +09:00
|
|
|
// 4. code가 없거나 교환 실패 시 기본 에러 페이지로 리다이렉트
|
2026-02-03 10:51:22 +09:00
|
|
|
const errorMessage = encodeURIComponent(
|
2026-02-04 09:35:42 +09:00
|
|
|
AUTH_ERROR_MESSAGES.INVALID_AUTH_LINK,
|
|
|
|
|
);
|
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
`${origin}${AUTH_ROUTES.LOGIN}?message=${errorMessage}`,
|
2026-02-03 10:51:22 +09:00
|
|
|
);
|
|
|
|
|
}
|