-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign-out-modal.tsx
87 lines (79 loc) · 2.17 KB
/
sign-out-modal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use client";
import { useState } from "react";
import { useRouter } from "next/router";
import { signOut } from "next-auth/react";
import { LogOut } from "lucide-react";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
interface SignOutModalProps {
children?: React.ReactNode;
}
const SignOutModal = ({ children }: SignOutModalProps) => {
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);
const [open, setOpen] = useState(false);
const handleSignOut = async () => {
try {
setIsLoading(true);
await signOut({
redirect: false,
callbackUrl: "/auth/sign-in",
});
router.push("/auth/sign-in");
} catch (error) {
console.error("Error during sign out:", error);
} finally {
setIsLoading(false);
setOpen(false);
}
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
{children || (
<Button variant="ghost" size="sm" className="w-full justify-start">
<LogOut className="mr-2 h-4 w-4" />
Logout
</Button>
)}
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Sign Out</DialogTitle>
<DialogDescription>
Are you sure you want to sign out of your account?
</DialogDescription>
</DialogHeader>
<DialogFooter className="mt-4">
<Button
variant="outline"
onClick={() => setOpen(false)}
disabled={isLoading}
tabIndex={0}
aria-label="Cancel sign out"
>
Cancel
</Button>
<Button
variant="destructive"
onClick={handleSignOut}
disabled={isLoading}
tabIndex={0}
aria-label="Confirm sign out"
>
{isLoading ? "Signing out..." : "Sign Out"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};
export default SignOutModal;