Skip to content

Commit

Permalink
feat: 회원탈퇴 고도화 (#81)
Browse files Browse the repository at this point in the history
* feat: apply alert

* feat: revalidatePath /sign-in

* feat: onDelete cascade
  • Loading branch information
bepyan committed Aug 21, 2024
1 parent 43b5f33 commit 39e6894
Show file tree
Hide file tree
Showing 10 changed files with 462 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/app/(auth)/sign-out/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use server";

import { revalidatePath } from "next/cache";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import {
Expand All @@ -16,6 +17,7 @@ export async function signOutAction(redirectUrl = "/") {
}

await invalidateAuth(session.id);
revalidatePath("/sign-in");

const headersList = headers();
const referer = headersList.get("referer") || "/";
Expand Down
23 changes: 22 additions & 1 deletion src/components/profile-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import { useMutation } from "@tanstack/react-query";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
import { useAlertDialogStore } from "~/components/global-alert";
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar";
import {
DropdownMenu,
Expand All @@ -20,17 +22,33 @@ export type ProfileDropDownProps = {
};

export default function ProfileDropDown({ user }: ProfileDropDownProps) {
const router = useRouter();
const openDialog = useAlertDialogStore((s) => s.openDialog);

const withdrawMutation = useMutation({
mutationFn: () => deleteUser(user!.id),
onSuccess: () => {
toast.success("회원탈퇴가 완료되었습니다.");
router.replace("/");
},
onError: (error) => {
console.error(error);
toast.error("회원탈퇴에 실패했습니다.");
},
});

const handleOnDelete = () => {
openDialog({
title: "정말 회원 탈퇴를 진행하시겠습니까?",
description: "이 작업은 되돌릴 수 없으며 모든 개인정보가 삭제됩니다.",
confirmText: "확인",
cancelText: "취소",
onConfirm: () => {
withdrawMutation.mutate();
},
});
};

return (
<div>
<DropdownMenu>
Expand All @@ -46,7 +64,10 @@ export default function ProfileDropDown({ user }: ProfileDropDownProps) {
<DropdownMenuItem asChild>
<Link href="/sign-out">로그아웃</Link>
</DropdownMenuItem>
<DropdownMenuItem onClick={() => withdrawMutation.mutate()}>
<DropdownMenuItem
className="text-destructive"
onClick={handleOnDelete}
>
탈퇴하기
</DropdownMenuItem>
</DropdownMenuContent>
Expand Down
24 changes: 24 additions & 0 deletions src/lib/db/migrations/0003_flippant_the_professor.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ALTER TABLE "session" DROP CONSTRAINT "session_user_id_user_id_fk";
--> statement-breakpoint
ALTER TABLE "invitation_response" DROP CONSTRAINT "invitation_response_invitation_id_invitation_id_fk";
--> statement-breakpoint
ALTER TABLE "invitation" DROP CONSTRAINT "invitation_user_id_user_id_fk";
--> statement-breakpoint
ALTER TABLE "invitation_response" ALTER COLUMN "invitation_id" SET NOT NULL;--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "invitation_response" ADD CONSTRAINT "invitation_response_invitation_id_invitation_id_fk" FOREIGN KEY ("invitation_id") REFERENCES "public"."invitation"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "invitation" ADD CONSTRAINT "invitation_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading

0 comments on commit 39e6894

Please sign in to comment.