diff --git a/src/app/(page)/mypage/page.tsx b/src/app/(page)/mypage/page.tsx index 4c5d70f..28e8f20 100644 --- a/src/app/(page)/mypage/page.tsx +++ b/src/app/(page)/mypage/page.tsx @@ -25,7 +25,6 @@ export default function MyPage() { const router = useRouter(); const { data: user } = useUserMe(); const [logoutOpen, setLogoutOpen] = useState(false); - const [withdrawOpen, setWithdrawOpen] = useState(false); const displayName = user?.name ?? user?.nickname ?? ''; @@ -33,11 +32,6 @@ export default function MyPage() { signOut({ callbackUrl: '/login' }); }; - const handleWithdraw = () => { - setWithdrawOpen(false); - router.push('/mypage/withdraw'); - }; - return ( <>
@@ -73,7 +67,7 @@ export default function MyPage() { + + setConfirmOpen(false)} + onConfirm={handleWithdraw} + /> ); } diff --git a/src/app/api/user/me/route.ts b/src/app/api/user/me/route.ts index 4583b97..f2e0f43 100644 --- a/src/app/api/user/me/route.ts +++ b/src/app/api/user/me/route.ts @@ -268,3 +268,56 @@ export async function PATCH(request: NextRequest) { ) } } + +export async function DELETE() { + const session = await getServerSession(authOptions) + const userId = getSessionUserId(session) + + if (!userId) return getAuthErrorResponse() + + try { + const user = await prisma.user.findUnique({ + where: { id: userId }, + select: { id: true, deletedAt: true }, + }) + + if (!user) { + return NextResponse.json( + { success: false, error: { code: 'USER_NOT_FOUND', message: '사용자를 찾을 수 없습니다.' } }, + { status: 404 }, + ) + } + + if (user.deletedAt) { + return NextResponse.json( + { success: false, error: { code: 'ALREADY_DELETED', message: '이미 탈퇴 처리된 계정입니다.' } }, + { status: 409 }, + ) + } + + const now = new Date() + + await prisma.$transaction([ + prisma.user.update({ + where: { id: userId }, + data: { deletedAt: now, deletionRequestedAt: now }, + }), + prisma.auditLog.create({ + data: { + eventType: 'USER_DELETED', + actorUserId: userId, + targetUserId: userId, + }, + }), + ]) + + return NextResponse.json({ success: true }) + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + console.error('[user/me] DELETE error', { message }) + return NextResponse.json( + { success: false, error: { code: 'INTERNAL_SERVER_ERROR', message: '서버 오류가 발생했습니다.' } }, + { status: 500 }, + ) + } +} diff --git a/src/lib/auth/index.ts b/src/lib/auth/index.ts index 871e35c..2e30246 100644 --- a/src/lib/auth/index.ts +++ b/src/lib/auth/index.ts @@ -124,9 +124,13 @@ export const authOptions: NextAuthOptions = { if (account?.provider === 'kakao' && user.id && UUID_REGEX.test(user.id)) { const existing = await prisma.user.findUnique({ where: { id: user.id }, - select: { kakaoId: true }, + select: { kakaoId: true, deletedAt: true }, }) + if (existing?.deletedAt) { + return false + } + if (!existing?.kakaoId) { await saveFirstLoginFields(user.id, account.providerAccountId) }