refactor: 인증 페이지를 React Hook Form 컴포넌트로 마이그레이션

- signup/page.tsx: SignupForm 컴포넌트 사용
- login/page.tsx: LoginForm 컴포넌트 사용
- reset-password/page.tsx: ResetPasswordForm 컴포넌트 사용
- auth/callback/route.ts: 불필요한 주석 제거
This commit is contained in:
2026-02-04 09:35:42 +09:00
parent 462d3c1923
commit 63a09034a9
4 changed files with 59 additions and 277 deletions

View File

@@ -1,57 +1,74 @@
import { createClient } from "@/utils/supabase/server";
import { NextResponse } from "next/server";
import { AUTH_ERROR_MESSAGES, AUTH_ROUTES } from "@/features/auth/constants";
/**
* [인증 콜백 라우트 핸들러]
*
* Supabase 인증 이메일(회원가입 확인, 비밀번호 재설정 등)에서
* Supabase 인증 이메일(회원가입 확인, 비밀번호 재설정 등) 및 OAuth(소셜 로그인)
* 리다이렉트될 때 호출되는 API 라우트입니다.
*
* PKCE(Proof Key for Code Exchange) 흐름:
* 1. 사용자가 이메일 링크 클릭
* 2. Supabase 서버가 토큰 검증 후 이 라우트로 `code` 파라미터와 함께 리다이렉트
* 3. 이 라우트에서 `code`를 세션으로 교환
* 4. 원래 목적지(next 파라미터)로 리다이렉트
*
* @param request - Next.js Request 객체
*/
export async function GET(request: Request) {
const { searchParams, origin } = new URL(request.url);
// 1. URL에서 code와 next(리다이렉트 목적지) 추출
// 1. URL에서 주요 직접 파라미터 및 에러 추출
const code = searchParams.get("code");
const next = searchParams.get("next") ?? "/";
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. code가 있으면 세션으로 교환
// 2. 인증 오류가 있는 경우 (예: 구글 로그인 취소 등)
if (error) {
console.error("Auth callback error parameter:", {
error,
error_code,
error_description,
});
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가 있으면 세션으로 교환 (정상 플로우)
if (code) {
const supabase = await createClient();
const { error } = await supabase.auth.exchangeCodeForSession(code);
const { error: exchangeError } =
await supabase.auth.exchangeCodeForSession(code);
if (!error) {
// 3. 세션 교환 성공 - 원래 목적지로 리다이렉트
// next가 절대 URL(http://...)이면 그대로 사용, 아니면 origin + next
const forwardedHost = request.headers.get("x-forwarded-host"); // 프록시 환경 대응
if (!exchangeError) {
// 세션 교환 성공 - 원래 목적지로 리다이렉트
const forwardedHost = request.headers.get("x-forwarded-host");
const isLocalEnv = process.env.NODE_ENV === "development";
if (isLocalEnv) {
// 개발 환경: localhost 사용
return NextResponse.redirect(`${origin}${next}`);
} else if (forwardedHost) {
// 프로덕션 + 프록시: x-forwarded-host 사용
return NextResponse.redirect(`https://${forwardedHost}${next}`);
} else {
// 프로덕션: origin 사용
return NextResponse.redirect(`${origin}${next}`);
}
}
// 에러 발생 시 로그 출력
console.error("Auth callback error:", error.message);
// 세션 교환 실패 시 로그 및 에러 메시지 설정
console.error("Auth exchange error:", exchangeError.message);
}
// 4. code가 없거나 교환 실패 시 에러 페이지로 리다이렉트
// 4. code가 없거나 교환 실패 시 기본 에러 페이지로 리다이렉트
const errorMessage = encodeURIComponent(
"인증 링크가 만료되었거나 유효하지 않습니다.",
AUTH_ERROR_MESSAGES.INVALID_AUTH_LINK,
);
return NextResponse.redirect(
`${origin}${AUTH_ROUTES.LOGIN}?message=${errorMessage}`,
);
return NextResponse.redirect(`${origin}/login?message=${errorMessage}`);
}

View File

@@ -1,13 +1,4 @@
import Link from "next/link";
import {
login,
signInWithGoogle,
signInWithKakao,
} from "@/features/auth/actions";
import FormMessage from "@/components/form-message";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Card,
CardContent,
@@ -15,8 +6,7 @@ import {
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Checkbox } from "@/components/ui/checkbox";
import { Separator } from "@/components/ui/separator";
import LoginForm from "@/features/auth/components/login-form";
/**
* [로그인 페이지 컴포넌트]
@@ -85,148 +75,8 @@ export default async function LoginPage({
</CardHeader>
{/* ========== 카드 콘텐츠 영역 (폼) ========== */}
<CardContent className="space-y-6">
{/* 로그인 폼 - formAction으로 서버 액션(login) 연결 */}
<form className="space-y-5">
{/* ========== 이메일 입력 필드 ========== */}
<div className="space-y-2">
{/* htmlFor와 Input의 id 연결로 접근성 향상 */}
<Label htmlFor="email" className="text-sm font-medium">
</Label>
{/* autoComplete="email": 브라우저 자동완성 기능 활성화 */}
{/* required: HTML5 필수 입력 검증 */}
<Input
id="email"
name="email"
type="email"
placeholder="your@email.com"
autoComplete="email"
required
className="h-11 transition-all duration-200"
/>
</div>
{/* ========== 비밀번호 입력 필드 ========== */}
<div className="space-y-2">
<Label htmlFor="password" className="text-sm font-medium">
</Label>
{/* pattern: 최소 8자, 대문자, 소문자, 숫자, 특수문자 각 1개 이상 */}
{/* 참고: HTML pattern에서는 <, >, {, } 등 일부 특수문자 사용 시 브라우저 호환성 문제 발생 */}
<Input
id="password"
name="password"
type="password"
placeholder="••••••••"
autoComplete="current-password"
required
minLength={8}
pattern="^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,}$"
title="비밀번호는 최소 8자 이상, 대문자, 소문자, 숫자, 특수문자를 각각 1개 이상 포함해야 합니다."
className="h-11 transition-all duration-200"
/>
</div>
{/* 로그인 유지 & 비밀번호 찾기 */}
<div className="flex items-center justify-between">
<div className="flex items-center space-x-2">
<Checkbox id="remember" name="remember-me" />
<Label
htmlFor="remember"
className="cursor-pointer text-sm font-normal leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
</Label>
</div>
{/* 비밀번호 찾기 링크 */}
<Link
href="/forgot-password"
className="text-sm font-medium text-gray-700 hover:text-black dark:text-gray-300 dark:hover:text-white"
>
</Link>
</div>
{/* 로그인 버튼 */}
<Button
formAction={login}
type="submit"
className="h-11 w-full bg-gradient-to-r from-gray-900 to-black font-semibold shadow-lg transition-all duration-200 hover:from-black hover:to-gray-800 hover:shadow-xl dark:from-white dark:to-gray-100 dark:text-black dark:hover:from-gray-100 dark:hover:to-white"
size="lg"
>
</Button>
{/* 회원가입 링크 */}
<p className="text-center text-sm text-gray-600 dark:text-gray-400">
?{" "}
<Link
href="/signup"
className="font-semibold text-gray-900 transition-colors hover:text-black dark:text-gray-100 dark:hover:text-white"
>
</Link>
</p>
</form>
{/* 소셜 로그인 구분선 */}
<div className="relative">
<Separator className="my-6" />
<span className="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 bg-white px-3 text-xs font-medium text-gray-500 dark:bg-gray-900 dark:text-gray-400">
</span>
</div>
{/* 소셜 로그인 버튼들 */}
<div className="grid grid-cols-2 gap-3">
{/* ========== Google 로그인 버튼 ========== */}
<form action={signInWithGoogle}>
<Button
type="submit"
variant="outline"
className="h-11 w-full border-gray-200 bg-white shadow-sm transition-all duration-200 hover:bg-gray-50 hover:shadow-md dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-750"
>
<svg className="mr-2 h-5 w-5" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
/>
<path
fill="currentColor"
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
/>
<path
fill="currentColor"
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
/>
<path
fill="currentColor"
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
/>
</svg>
Google
</Button>
</form>
{/* ========== Kakao 로그인 버튼 ========== */}
<form action={signInWithKakao}>
<Button
type="submit"
variant="outline"
className="h-11 w-full border-none bg-[#FEE500] font-semibold text-[#3C1E1E] shadow-sm transition-all duration-200 hover:bg-[#FDD835] hover:shadow-md"
>
<svg
className="mr-2 h-5 w-5"
viewBox="0 0 24 24"
fill="currentColor"
>
<path d="M12 3c-4.97 0-9 4.03-9 9s4.03 9 9 9 9-4.03 9-9-4.03-9-9-9zm-1.5 14.5h-3v-9h3v9zm3 0h-3v-5h3v5zm0-6h-3v-3h3v3z" />
</svg>
Kakao
</Button>
</form>
</div>
<CardContent>
<LoginForm />
</CardContent>
</Card>
</div>

View File

@@ -1,8 +1,5 @@
import FormMessage from "@/components/form-message";
import { updatePassword } from "@/features/auth/actions";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import ResetPasswordForm from "@/features/auth/components/reset-password-form";
import {
Card,
CardContent,
@@ -105,38 +102,7 @@ export default async function ResetPasswordPage({
{/* ========== 폼 영역 ========== */}
<CardContent className="space-y-6">
{/* 비밀번호 업데이트 폼 */}
<form className="space-y-5">
{/* ========== 새 비밀번호 입력 ========== */}
<div className="space-y-2">
<Label htmlFor="password" className="text-sm font-medium">
</Label>
<Input
id="password"
name="password"
type="password"
placeholder="••••••••"
autoComplete="new-password"
required
minLength={8}
pattern="^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,}$"
title="비밀번호는 최소 8자 이상, 대문자, 소문자, 숫자, 특수문자를 각각 1개 이상 포함해야 합니다."
className="h-11 transition-all duration-200"
/>
<p className="text-xs text-gray-500 dark:text-gray-400">
8 , , , ,
</p>
</div>
{/* ========== 비밀번호 변경 버튼 ========== */}
<Button
formAction={updatePassword}
className="h-11 w-full bg-gradient-to-r from-gray-900 to-black font-semibold text-white shadow-lg transition-all hover:from-black hover:to-gray-800 hover:shadow-xl dark:from-white dark:to-gray-100 dark:text-black dark:hover:from-gray-100 dark:hover:to-white"
>
</Button>
</form>
<ResetPasswordForm />
</CardContent>
</Card>
</div>

View File

@@ -1,9 +1,6 @@
import Link from "next/link";
import { signup } from "@/features/auth/actions";
import FormMessage from "@/components/form-message";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import SignupForm from "@/features/auth/components/signup-form";
import {
Card,
CardContent,
@@ -46,58 +43,11 @@ export default async function SignupPage({
</CardDescription>
</CardHeader>
{/* ========== 폼 영역 ========== */}
<CardContent className="space-y-6">
<form className="space-y-5">
{/* 이메일 입력 */}
<div className="space-y-2">
<Label htmlFor="email" className="text-sm font-medium">
</Label>
<Input
id="email"
name="email"
type="email"
placeholder="your@email.com"
required
className="h-11 transition-all duration-200"
/>
</div>
<SignupForm />
{/* 비밀번호 입력 */}
<div className="space-y-2">
<Label htmlFor="password" className="text-sm font-medium">
</Label>
{/* pattern: 최소 8자, 대문자, 소문자, 숫자, 특수문자 각 1개 이상 */}
{/* 참고: HTML pattern에서는 <, >, {, } 등 일부 특수문자 사용 시 브라우저 호환성 문제 발생 */}
<Input
id="password"
name="password"
type="password"
placeholder="••••••••"
autoComplete="new-password"
required
minLength={6}
pattern="^(?=.*[0-9])(?=.*[!@#$%^&*]).{6,}$"
title="비밀번호는 최소 6자 이상, 숫자와 특수문자를 각각 1개 이상 포함해야 합니다."
className="h-11 transition-all duration-200"
/>
<p className="text-xs text-gray-500 dark:text-gray-400">
6 , , ( )
</p>
</div>
{/* 회원가입 버튼 */}
<Button
formAction={signup}
type="submit"
className="h-11 w-full bg-gradient-to-r from-gray-900 to-black font-semibold shadow-lg transition-all duration-200 hover:from-black hover:to-gray-800 hover:shadow-xl dark:from-white dark:to-gray-100 dark:text-black dark:hover:from-gray-100 dark:hover:to-white"
size="lg"
>
</Button>
{/* 로그인 링크 */}
{/* ========== 로그인 링크 ========== */}
<p className="text-center text-sm text-gray-600 dark:text-gray-400">
?{" "}
<Link
@@ -107,7 +57,6 @@ export default async function SignupPage({
</Link>
</p>
</form>
</CardContent>
</Card>
</div>