✨ feat: add logout feature
All checks were successful
Integration Tests / integration-tests (pull_request) Successful in 1m44s
All checks were successful
Integration Tests / integration-tests (pull_request) Successful in 1m44s
This commit is contained in:
23
shared/models/auth/logout.ts
Normal file
23
shared/models/auth/logout.ts
Normal file
@ -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",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -6,12 +6,11 @@ import {
|
|||||||
AlertDialogDescription,
|
AlertDialogDescription,
|
||||||
AlertDialogFooter,
|
AlertDialogFooter,
|
||||||
AlertDialogHeader,
|
AlertDialogHeader,
|
||||||
AlertDialogMedia,
|
|
||||||
AlertDialogTitle,
|
AlertDialogTitle,
|
||||||
} from "@/shared/libs/shadcn/ui/alert-dialog";
|
} from "@/shared/libs/shadcn/ui/alert-dialog";
|
||||||
import { Spinner } from "@/shared/libs/shadcn/ui/spinner";
|
import { Spinner } from "@/shared/libs/shadcn/ui/spinner";
|
||||||
|
import { logout } from "@/shared/models/auth/logout";
|
||||||
import { Button } from "@base-ui/react";
|
import { Button } from "@base-ui/react";
|
||||||
import { LogOut } from "lucide-react";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
const LogoutAlert = ({
|
const LogoutAlert = ({
|
||||||
@ -22,6 +21,12 @@ const LogoutAlert = ({
|
|||||||
setOpenState: React.Dispatch<React.SetStateAction<boolean>>;
|
setOpenState: React.Dispatch<React.SetStateAction<boolean>>;
|
||||||
}) => {
|
}) => {
|
||||||
const [isLoading, setIsLoading] = React.useState(false);
|
const [isLoading, setIsLoading] = React.useState(false);
|
||||||
|
const continueLogout = async () => {
|
||||||
|
setIsLoading(true);
|
||||||
|
await logout().then((res) =>
|
||||||
|
res.success ? window.location.reload() : setIsLoading(false),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AlertDialog open={openState}>
|
<AlertDialog open={openState}>
|
||||||
@ -46,7 +51,7 @@ const LogoutAlert = ({
|
|||||||
<Button
|
<Button
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
className="w-full hover:cursor-pointer"
|
className="w-full hover:cursor-pointer"
|
||||||
onClick={() => setIsLoading(true)}
|
onClick={continueLogout}
|
||||||
>
|
>
|
||||||
{isLoading && <Spinner />}
|
{isLoading && <Spinner />}
|
||||||
Logout
|
Logout
|
||||||
|
|||||||
Reference in New Issue
Block a user