🥅 fix: handle logout failure warning
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
import type { Metadata } from "next";
|
import type { Metadata } from "next";
|
||||||
import { Geist, Geist_Mono, Inter } from "next/font/google";
|
import { Geist, Geist_Mono, Inter } from "next/font/google";
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
|
import { Toaster } from "@/shared/libs/shadcn/ui/sonner";
|
||||||
|
|
||||||
const inter = Inter({ subsets: ["latin"], variable: "--font-sans" });
|
const inter = Inter({ subsets: ["latin"], variable: "--font-sans" });
|
||||||
|
|
||||||
@ -29,7 +30,8 @@ export default function RootLayout({
|
|||||||
<body
|
<body
|
||||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||||
>
|
>
|
||||||
{children}
|
<main>{children}</main>
|
||||||
|
<Toaster />
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
|
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
export const logout = async () => {
|
export const logout = async () => {
|
||||||
const res = (await backendFetch("auth/logout", {
|
const res = (await backendFetch("auth/logout", {
|
||||||
@ -9,11 +10,7 @@ export const logout = async () => {
|
|||||||
})) as BackendResponse;
|
})) as BackendResponse;
|
||||||
|
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
(await cookies()).delete("auth_token");
|
redirect("/auth/logout");
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
message: "Logged out successfully",
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import { Spinner } from "@/shared/libs/shadcn/ui/spinner";
|
|||||||
import { logout } from "@/shared/models/auth/logout";
|
import { logout } from "@/shared/models/auth/logout";
|
||||||
import { Button } from "@base-ui/react";
|
import { Button } from "@base-ui/react";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
const LogoutAlert = ({
|
const LogoutAlert = ({
|
||||||
openState,
|
openState,
|
||||||
@ -23,9 +24,16 @@ const LogoutAlert = ({
|
|||||||
const [isLoading, setIsLoading] = React.useState(false);
|
const [isLoading, setIsLoading] = React.useState(false);
|
||||||
const continueLogout = async () => {
|
const continueLogout = async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
await logout().then((res) =>
|
const res = await logout();
|
||||||
res.success ? window.location.reload() : setIsLoading(false),
|
if (!res.success) {
|
||||||
);
|
setIsLoading(false);
|
||||||
|
toast.error(res.message || "Logout failed", {
|
||||||
|
position: "bottom-right",
|
||||||
|
description:
|
||||||
|
"An error occurred while logging out. Please try again later.",
|
||||||
|
richColors: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user