From 36ad865c337241e1a0ab7170a0ce0eada0a23c31 Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Sat, 14 Feb 2026 21:37:06 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20logout=20feature?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/models/auth/logout.ts | 23 +++++++++++++++++++ .../widgets/navbar/components/LogoutAlert.tsx | 11 ++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 shared/models/auth/logout.ts diff --git a/shared/models/auth/logout.ts b/shared/models/auth/logout.ts new file mode 100644 index 0000000..4e01ddb --- /dev/null +++ b/shared/models/auth/logout.ts @@ -0,0 +1,23 @@ +"use server"; + +import { backendFetch } from "@/shared/helpers/backendFetch"; +import { cookies } from "next/headers"; + +export const logout = async () => { + const res = await backendFetch("auth/logout", { + method: "POST", + }); + + if (res.success) { + (await cookies()).delete("auth_token"); + return { + success: true, + message: "Logged out successfully", + }; + } else { + return { + success: false, + message: "Logout failed", + }; + } +}; diff --git a/shared/widgets/navbar/components/LogoutAlert.tsx b/shared/widgets/navbar/components/LogoutAlert.tsx index 1e5b254..b15bd34 100644 --- a/shared/widgets/navbar/components/LogoutAlert.tsx +++ b/shared/widgets/navbar/components/LogoutAlert.tsx @@ -6,12 +6,11 @@ import { AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, - AlertDialogMedia, AlertDialogTitle, } from "@/shared/libs/shadcn/ui/alert-dialog"; import { Spinner } from "@/shared/libs/shadcn/ui/spinner"; +import { logout } from "@/shared/models/auth/logout"; import { Button } from "@base-ui/react"; -import { LogOut } from "lucide-react"; import React from "react"; const LogoutAlert = ({ @@ -22,6 +21,12 @@ const LogoutAlert = ({ setOpenState: React.Dispatch>; }) => { const [isLoading, setIsLoading] = React.useState(false); + const continueLogout = async () => { + setIsLoading(true); + await logout().then((res) => + res.success ? window.location.reload() : setIsLoading(false), + ); + }; return ( @@ -46,7 +51,7 @@ const LogoutAlert = ({