2026-02-05 09:38:42 +09:00
|
|
|
|
import { createClient } from "@/utils/supabase/server";
|
|
|
|
|
|
import {
|
|
|
|
|
|
AUTH_ERROR_MESSAGES,
|
|
|
|
|
|
AUTH_ROUTES,
|
|
|
|
|
|
RECOVERY_COOKIE_MAX_AGE_SECONDS,
|
|
|
|
|
|
RECOVERY_COOKIE_NAME,
|
|
|
|
|
|
} from "@/features/auth/constants";
|
|
|
|
|
|
import { getAuthErrorMessage } from "@/features/auth/errors";
|
2026-02-03 10:51:22 +09:00
|
|
|
|
import { type EmailOtpType } from "@supabase/supabase-js";
|
2026-02-05 09:38:42 +09:00
|
|
|
|
import { NextResponse, type NextRequest } from "next/server";
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
const RESET_PASSWORD_PATH = AUTH_ROUTES.RESET_PASSWORD;
|
|
|
|
|
|
const LOGIN_PATH = AUTH_ROUTES.LOGIN;
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-05 09:38:42 +09:00
|
|
|
|
* 이메일 인증(/auth/confirm) 처리
|
|
|
|
|
|
* - token_hash + type 검증
|
|
|
|
|
|
* - recovery 타입일 경우 세션 쿠키 설정 후 비밀번호 재설정 페이지로 리다이렉트
|
2026-02-03 10:51:22 +09:00
|
|
|
|
*/
|
|
|
|
|
|
export async function GET(request: NextRequest) {
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// 1) 이메일 링크에 들어있는 값 읽기
|
2026-02-03 10:51:22 +09:00
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// token_hash: 인증에 필요한 값
|
|
|
|
|
|
// type: 어떤 인증인지 구분 (예: 가입, 비밀번호 재설정)
|
2026-02-03 10:51:22 +09:00
|
|
|
|
const tokenHash = searchParams.get("token_hash");
|
|
|
|
|
|
const type = searchParams.get("type") as EmailOtpType | null;
|
|
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// redirect_to/next: 인증 후에 이동할 주소
|
|
|
|
|
|
const rawRedirect =
|
|
|
|
|
|
searchParams.get("redirect_to") ?? searchParams.get("next");
|
|
|
|
|
|
|
|
|
|
|
|
// 보안상 우리 사이트 안 경로(`/...`)만 허용
|
|
|
|
|
|
const safeRedirect =
|
|
|
|
|
|
rawRedirect && rawRedirect.startsWith("/") ? rawRedirect : null;
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// 일반 인증이 끝난 뒤 이동할 경로
|
|
|
|
|
|
const nextPath = safeRedirect ?? AUTH_ROUTES.HOME;
|
|
|
|
|
|
// 비밀번호 재설정일 때 이동할 경로
|
|
|
|
|
|
const recoveryPath = safeRedirect ?? RESET_PASSWORD_PATH;
|
|
|
|
|
|
|
|
|
|
|
|
// 필수 값이 없으면 로그인으로 보내고 에러를 보여줌
|
2026-02-03 10:51:22 +09:00
|
|
|
|
if (!tokenHash || !type) {
|
2026-02-05 09:38:42 +09:00
|
|
|
|
return redirectWithError(request, AUTH_ERROR_MESSAGES.INVALID_AUTH_LINK);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// 2) Supabase에게 "이 링크가 맞는지" 확인 요청
|
2026-02-03 10:51:22 +09:00
|
|
|
|
const supabase = await createClient();
|
|
|
|
|
|
const { error } = await supabase.auth.verifyOtp({
|
|
|
|
|
|
type,
|
|
|
|
|
|
token_hash: tokenHash,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// 확인 실패 시 이유를 알기 쉬운 메시지로 보여줌
|
2026-02-03 10:51:22 +09:00
|
|
|
|
if (error) {
|
2026-02-05 09:38:42 +09:00
|
|
|
|
console.error("[Auth Confirm] verifyOtp error:", error.message);
|
|
|
|
|
|
const message = getAuthErrorMessage(error);
|
|
|
|
|
|
return redirectWithError(request, message);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// 3) 비밀번호 재설정이면 재설정 페이지로 보내고 쿠키를 저장
|
2026-02-03 10:51:22 +09:00
|
|
|
|
if (type === "recovery") {
|
2026-02-05 09:38:42 +09:00
|
|
|
|
const response = NextResponse.redirect(new URL(recoveryPath, request.url));
|
|
|
|
|
|
response.cookies.set(RECOVERY_COOKIE_NAME, "1", {
|
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
|
sameSite: "lax",
|
|
|
|
|
|
secure: process.env.NODE_ENV === "production",
|
|
|
|
|
|
maxAge: RECOVERY_COOKIE_MAX_AGE_SECONDS,
|
|
|
|
|
|
path: "/",
|
|
|
|
|
|
});
|
|
|
|
|
|
return response;
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// 4) 그 외 인증은 기본 경로로 이동
|
|
|
|
|
|
return NextResponse.redirect(new URL(nextPath, request.url));
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 09:38:42 +09:00
|
|
|
|
// 로그인 페이지로 보내면서 에러 메시지를 함께 전달
|
|
|
|
|
|
function redirectWithError(request: NextRequest, message: string) {
|
2026-02-03 10:51:22 +09:00
|
|
|
|
const encodedMessage = encodeURIComponent(message);
|
2026-02-05 09:38:42 +09:00
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
|
new URL(`${LOGIN_PATH}?message=${encodedMessage}`, request.url),
|
|
|
|
|
|
);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|