2026-02-05 09:38:42 +09:00
|
|
|
|
import { createClient } from "@/utils/supabase/server";
|
2026-02-05 12:13:06 +09:00
|
|
|
|
import { type NextRequest, NextResponse } from "next/server"; // NextRequest 추가
|
2026-02-04 09:35:42 +09:00
|
|
|
|
import { AUTH_ERROR_MESSAGES, AUTH_ROUTES } from "@/features/auth/constants";
|
2026-02-05 09:38:42 +09:00
|
|
|
|
import { getAuthErrorMessage } from "@/features/auth/errors";
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-05 12:13:06 +09:00
|
|
|
|
* OAuth/이메일 인증 콜백 처리
|
|
|
|
|
|
*
|
|
|
|
|
|
* Supabase 인증 후 리다이렉트되는 라우트입니다.
|
|
|
|
|
|
* - 인증 코드를 세션으로 교환합니다.
|
|
|
|
|
|
* - 인증 에러를 처리합니다.
|
|
|
|
|
|
* - 최종 목적지(Next URL)로 리다이렉트합니다.
|
2026-02-03 10:51:22 +09:00
|
|
|
|
*/
|
2026-02-05 12:13:06 +09:00
|
|
|
|
export async function GET(request: NextRequest) {
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
// 1. 요청 파라미터 및 URL 준비
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
const requestUrl = request.nextUrl.clone(); // URL 조작을 위해 복제
|
|
|
|
|
|
const code = requestUrl.searchParams.get("code");
|
|
|
|
|
|
const next = requestUrl.searchParams.get("next") ?? AUTH_ROUTES.HOME;
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// 에러 파라미터 확인
|
|
|
|
|
|
const error = requestUrl.searchParams.get("error");
|
|
|
|
|
|
const error_code = requestUrl.searchParams.get("error_code");
|
|
|
|
|
|
const error_description = requestUrl.searchParams.get("error_description");
|
|
|
|
|
|
const origin = requestUrl.origin;
|
2026-02-04 09:35:42 +09:00
|
|
|
|
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
// 2. 초기 에러 처리 (Provider 레벨 에러)
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2026-02-04 09:35:42 +09:00
|
|
|
|
if (error) {
|
|
|
|
|
|
console.error("Auth callback error parameter:", {
|
|
|
|
|
|
error,
|
|
|
|
|
|
error_code,
|
|
|
|
|
|
error_description,
|
|
|
|
|
|
});
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
2026-02-04 12:28:15 +09:00
|
|
|
|
let message: string = AUTH_ERROR_MESSAGES.DEFAULT;
|
2026-02-04 09:35:42 +09:00
|
|
|
|
|
|
|
|
|
|
if (error === "access_denied") {
|
|
|
|
|
|
message = AUTH_ERROR_MESSAGES.OAUTH_ACCESS_DENIED;
|
|
|
|
|
|
} else if (error === "server_error") {
|
|
|
|
|
|
message = AUTH_ERROR_MESSAGES.OAUTH_SERVER_ERROR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// 로그인 페이지로 에러와 함께 이동
|
2026-02-04 09:35:42 +09:00
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
|
`${origin}${AUTH_ROUTES.LOGIN}?message=${encodeURIComponent(message)}`,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
// 3. 인증 코드 교환 (Supabase 공식 패턴 적용)
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2026-02-03 10:51:22 +09:00
|
|
|
|
if (code) {
|
|
|
|
|
|
const supabase = await createClient();
|
2026-02-05 12:13:06 +09:00
|
|
|
|
|
|
|
|
|
|
// 코드 교환 실행
|
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) {
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
// 3-1. 교환 성공: 리다이렉트 처리
|
2026-02-05 13:19:53 +09:00
|
|
|
|
// code 교환으로 세션이 생성된 상태입니다.
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// ----------------------------------------------------------------------
|
2026-02-05 13:19:53 +09:00
|
|
|
|
|
2026-02-05 15:39:44 +09:00
|
|
|
|
// 회원가입 인증 여부 확인 (쿼리 파라미터 기반)
|
|
|
|
|
|
// actions.ts의 signup 함수에서 emailRedirectTo에 auth_type=signup을 추가해서 보냅니다.
|
|
|
|
|
|
const authType = requestUrl.searchParams.get("auth_type");
|
|
|
|
|
|
const isSignupVerification = authType === "signup";
|
2026-02-05 13:19:53 +09:00
|
|
|
|
|
|
|
|
|
|
// 회원가입 인증인 경우:
|
|
|
|
|
|
// 이메일 인증만 완료하고, 자동 로그인된 세션은 종료시킨 뒤 로그인 페이지로 보냅니다.
|
2026-02-05 15:39:44 +09:00
|
|
|
|
if (isSignupVerification) {
|
2026-02-05 13:19:53 +09:00
|
|
|
|
await supabase.auth.signOut();
|
|
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
|
`${origin}${AUTH_ROUTES.LOGIN}?message=${encodeURIComponent(
|
|
|
|
|
|
AUTH_ERROR_MESSAGES.EMAIL_VERIFIED_SUCCESS,
|
|
|
|
|
|
)}`,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 그 외 일반적인 로그인/인증인 경우:
|
|
|
|
|
|
// 코드 파라미터 등을 제거하고 깨끗한 URL로 이동합니다.
|
2026-02-05 12:13:06 +09:00
|
|
|
|
const forwardedHost = request.headers.get("x-forwarded-host"); // 로드밸런서 지원
|
2026-02-03 10:51:22 +09:00
|
|
|
|
const isLocalEnv = process.env.NODE_ENV === "development";
|
|
|
|
|
|
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// 리다이렉트할 최종 URL 설정
|
2026-02-03 10:51:22 +09:00
|
|
|
|
if (isLocalEnv) {
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// 로컬 개발 환경
|
2026-02-03 10:51:22 +09:00
|
|
|
|
return NextResponse.redirect(`${origin}${next}`);
|
2026-02-05 12:13:06 +09:00
|
|
|
|
} else if (forwardedHost) {
|
|
|
|
|
|
// 프로덕션 환경 (Vercel 등 프록시 뒤)
|
2026-02-03 10:51:22 +09:00
|
|
|
|
return NextResponse.redirect(`https://${forwardedHost}${next}`);
|
2026-02-05 12:13:06 +09:00
|
|
|
|
} else {
|
|
|
|
|
|
// 기본
|
|
|
|
|
|
return NextResponse.redirect(`${origin}${next}`);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
// 3-2. 교환 실패: 에러 처리
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
2026-02-04 09:35:42 +09:00
|
|
|
|
console.error("Auth exchange error:", exchangeError.message);
|
2026-02-05 09:38:42 +09:00
|
|
|
|
const message = getAuthErrorMessage(exchangeError);
|
|
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
|
`${origin}${AUTH_ROUTES.LOGIN}?message=${encodeURIComponent(message)}`,
|
|
|
|
|
|
);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 12:13:06 +09:00
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
// 4. 잘못된 접근 처리
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
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
|
|
|
|
);
|
|
|
|
|
|
}
|