fix logout

This commit is contained in:
2025-06-11 14:16:34 +08:00
parent 9dbcf94a34
commit 893c6cbe27
5 changed files with 61 additions and 40 deletions

View File

@@ -23,20 +23,19 @@ export async function GET() {
}
try {
const response = NextResponse.json({
success: true,
message: "Logout berhasil",
});
// Menghapus cookie dengan set maxAge 0
response.cookies.set({
cookieStore.set({
name: sessionKey,
value: "",
path: "/",
maxAge: 0,
});
return response;
return NextResponse.json({
success: true,
message: "Logout berhasil",
});
} catch (error) {
console.error("Gagal menghapus cookie:", error);
return NextResponse.json(

View File

@@ -2,21 +2,21 @@
import { AccentColor } from "@/app_modules/_global/color";
import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global";
import { apiFetchLogout } from "@/app_modules/auth/_lib/api_fetch_auth";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
import { MODEL_USER } from "@/app_modules/home/model/interface";
import { Warna } from "@/lib/warna";
import {
ActionIcon,
Button,
Center,
Divider,
Group,
Indicator,
MediaQuery,
Popover,
SimpleGrid,
Stack,
Text,
Title,
Title
} from "@mantine/core";
import {
IconBell,
@@ -30,8 +30,6 @@ import {
import { useRouter } from "next/navigation";
import { useState } from "react";
import { Admin_ComponentModal } from "../_admin_global/_component/comp_admin_modal";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
import { ComponentAdminGlobal_NotifikasiPeringatan } from "../_admin_global/admin_notifikasi/notifikasi_peringatan";
export function Admin_V3_ComponentButtonUserCircle({
dataUser,
@@ -134,15 +132,18 @@ export function Admin_V3_ComponentButtonUserCircle({
];
async function onClickLogout() {
setLoadingLogout(true);
const res = await fetch(`/api/auth/logout?id=${dataUser?.id}`, {
method: "GET",
});
try {
setLoadingLogout(true);
const response = await apiFetchLogout({ id: dataUser?.id as string });
const result = await res.json();
if (res.status === 200) {
ComponentGlobal_NotifikasiBerhasil(result.message);
router.push("/", { scroll: false });
if (response && response.success) {
ComponentGlobal_NotifikasiBerhasil(response.message);
router.replace("/", { scroll: false });
}
} catch (error) {
console.error(error);
} finally {
setLoadingLogout(false);
}
}
@@ -242,10 +243,13 @@ export function Admin_V3_ComponentButtonUserCircle({
<IconBell color="white" />
) : (
<Indicator
color="yellow"
processing
label={<Text c="black" fz={10}>{countNotifikasi ? countNotifikasi : ""}</Text>}
label={
<Text c="black" fz={10}>
{countNotifikasi ? countNotifikasi : ""}
</Text>
}
>
<IconBell color="white" />
</Indicator>
@@ -284,7 +288,11 @@ export function Admin_V3_ComponentButtonUserCircle({
<Indicator
color="yellow"
processing
label={<Text c="black" fz={10}>{countNotifikasi ? countNotifikasi : ""}</Text>}
label={
<Text c="black" fz={10}>
{countNotifikasi ? countNotifikasi : ""}
</Text>
}
>
<IconBell size={18} color={e.color || "white"} />
</Indicator>

View File

@@ -4,6 +4,7 @@ export {
apiPostVerifikasiCodeOtp,
apiDeleteAktivasiKodeOtpByNomor,
apiFetchRegister,
apiFetchLogout,
};
const apiFetchLogin = async ({ nomor }: { nomor: string }) => {
@@ -71,3 +72,14 @@ const apiFetchRegister = async ({
return result;
// return await respone.json().catch(() => null);
};
const apiFetchLogout = async ({ id }: { id: string }) => {
const respone = await fetch(`/api/auth/logout?id=${id}`, {
method: "GET",
});
const result = await respone.json();
return result;
};

View File

@@ -20,12 +20,12 @@ export default function InvalidUser() {
await fetch("/api/auth/logout", {
method: "GET",
});
router.push("/login", {scroll: false});
router.push("/", { scroll: false });
setIsLoading(false);
} catch (error) {
setIsLoading(false);
console.error("Gagal menghapus cookie:", error);
}
}
};
return (
@@ -33,7 +33,8 @@ export default function InvalidUser() {
<UIGlobal_LayoutDefault>
<Stack align="center" justify="center" spacing="md" h={"100vh"}>
<Title order={3} c={MainColor.white} align="center">
Maaf, Anda tidak memiliki izin untuk mengakses halaman ini. Silakan logout terlebih dahulu.
Maaf, Anda tidak memiliki izin untuk mengakses halaman ini. Silakan
logout terlebih dahulu.
</Title>
<Button
loading={isLoading}

View File

@@ -1,32 +1,33 @@
"use client";
import { Warna } from "@/lib/warna";
import { MainColor } from "@/app_modules/_global/color";
import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil";
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan";
import UIGlobal_Modal from "@/app_modules/_global/ui/ui_modal";
import { Warna } from "@/lib/warna";
import { ActionIcon, Button, Stack, Text } from "@mantine/core";
import { IconLogout } from "@tabler/icons-react";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { auth_Logout } from "../fun/fun_logout";
import { RouterAuth } from "@/lib/router_hipmi/router_auth";
import { MainColor } from "@/app_modules/_global/color";
import { apiFetchLogout } from "../_lib/api_fetch_auth";
export default function Component_ButtonLogout({userId}: {userId: string}) {
export default function Component_ButtonLogout({ userId }: { userId: string }) {
const router = useRouter();
const [opened, setOpened] = useState(false);
const [loading, setLoading] = useState(false);
async function onClickLogout() {
setLoading(true);
const res = await fetch(`/api/auth/logout?id=${userId}`, {
method: "GET",
});
try {
setLoading(true);
const response = await apiFetchLogout({ id: userId });
const result = await res.json();
if (res.status === 200) {
ComponentGlobal_NotifikasiBerhasil(result.message);
router.push("/", { scroll: false });
if (response && response.success) {
ComponentGlobal_NotifikasiBerhasil(response.message);
router.replace("/", { scroll: false });
}
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
}