회원가입
This commit is contained in:
76
app/auth/confirm/route.ts
Normal file
76
app/auth/confirm/route.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import { AUTH_ERROR_MESSAGES } from "@/features/auth/constants";
|
||||
import { type EmailOtpType } from "@supabase/supabase-js";
|
||||
import { redirect } from "next/navigation";
|
||||
import { type NextRequest } from "next/server";
|
||||
|
||||
// ========================================
|
||||
// 상수 정의
|
||||
// ========================================
|
||||
|
||||
/** 비밀번호 재설정 후 이동할 경로 */
|
||||
const RESET_PASSWORD_PATH = "/reset-password";
|
||||
|
||||
/** 인증 실패 시 리다이렉트할 경로 */
|
||||
const LOGIN_PATH = "/login";
|
||||
|
||||
// ========================================
|
||||
// 라우트 핸들러
|
||||
// ========================================
|
||||
|
||||
/**
|
||||
* [이메일 인증 확인 라우트]
|
||||
*
|
||||
* Supabase 이메일 템플릿의 인증 링크를 처리합니다.
|
||||
* - 회원가입 이메일 확인
|
||||
* - 비밀번호 재설정
|
||||
*
|
||||
* @example Supabase 이메일 템플릿 형식
|
||||
* {{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=recovery
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
// ========== 파라미터 추출 ==========
|
||||
const tokenHash = searchParams.get("token_hash");
|
||||
const type = searchParams.get("type") as EmailOtpType | null;
|
||||
const rawNext = searchParams.get("next");
|
||||
|
||||
// 보안: 외부 URL 리다이렉트 방지 (상대 경로만 허용)
|
||||
const nextPath = rawNext?.startsWith("/") ? rawNext : "/";
|
||||
|
||||
// ========== 토큰 검증 ==========
|
||||
if (!tokenHash || !type) {
|
||||
return redirectWithError(AUTH_ERROR_MESSAGES.INVALID_AUTH_LINK);
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
const { error } = await supabase.auth.verifyOtp({
|
||||
type,
|
||||
token_hash: tokenHash,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error("[Auth Confirm] verifyOtp 실패:", error.message);
|
||||
return redirectWithError(AUTH_ERROR_MESSAGES.INVALID_AUTH_LINK);
|
||||
}
|
||||
|
||||
// ========== 검증 성공 - 적절한 페이지로 리다이렉트 ==========
|
||||
if (type === "recovery") {
|
||||
redirect(RESET_PASSWORD_PATH);
|
||||
}
|
||||
|
||||
redirect(nextPath);
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// 헬퍼 함수
|
||||
// ========================================
|
||||
|
||||
/**
|
||||
* 에러 메시지와 함께 로그인 페이지로 리다이렉트
|
||||
*/
|
||||
function redirectWithError(message: string): never {
|
||||
const encodedMessage = encodeURIComponent(message);
|
||||
redirect(`${LOGIN_PATH}?message=${encodedMessage}`);
|
||||
}
|
||||
Reference in New Issue
Block a user