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 { try {
const response = NextResponse.json({
success: true,
message: "Logout berhasil",
});
// Menghapus cookie dengan set maxAge 0 // Menghapus cookie dengan set maxAge 0
response.cookies.set({ cookieStore.set({
name: sessionKey, name: sessionKey,
value: "", value: "",
path: "/", path: "/",
maxAge: 0, maxAge: 0,
}); });
return response; return NextResponse.json({
success: true,
message: "Logout berhasil",
});
} catch (error) { } catch (error) {
console.error("Gagal menghapus cookie:", error); console.error("Gagal menghapus cookie:", error);
return NextResponse.json( return NextResponse.json(

View File

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

View File

@@ -4,6 +4,7 @@ export {
apiPostVerifikasiCodeOtp, apiPostVerifikasiCodeOtp,
apiDeleteAktivasiKodeOtpByNomor, apiDeleteAktivasiKodeOtpByNomor,
apiFetchRegister, apiFetchRegister,
apiFetchLogout,
}; };
const apiFetchLogin = async ({ nomor }: { nomor: string }) => { const apiFetchLogin = async ({ nomor }: { nomor: string }) => {
@@ -71,3 +72,14 @@ const apiFetchRegister = async ({
return result; return result;
// return await respone.json().catch(() => null); // 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,7 +20,7 @@ export default function InvalidUser() {
await fetch("/api/auth/logout", { await fetch("/api/auth/logout", {
method: "GET", method: "GET",
}); });
router.push("/login", {scroll: false}); router.push("/", { scroll: false });
setIsLoading(false); setIsLoading(false);
} catch (error) { } catch (error) {
setIsLoading(false); setIsLoading(false);
@@ -33,7 +33,8 @@ export default function InvalidUser() {
<UIGlobal_LayoutDefault> <UIGlobal_LayoutDefault>
<Stack align="center" justify="center" spacing="md" h={"100vh"}> <Stack align="center" justify="center" spacing="md" h={"100vh"}>
<Title order={3} c={MainColor.white} align="center"> <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> </Title>
<Button <Button
loading={isLoading} loading={isLoading}

View File

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