Skip to content

Commit 89fcf88

Browse files
committed
refactor: use GET /api/auth/logout for sidebar
1 parent e96b94e commit 89fcf88

File tree

1 file changed

+12
-40
lines changed

1 file changed

+12
-40
lines changed

components/nav-user.tsx

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import {
1111
DropdownMenuSeparator,
1212
DropdownMenuTrigger,
1313
} from "@/components/ui/dropdown-menu";
14-
import { SidebarMenu, SidebarMenuButton, SidebarMenuItem, useSidebar } from "@/components/ui/sidebar";
14+
import {
15+
SidebarMenu,
16+
SidebarMenuButton,
17+
SidebarMenuItem,
18+
useSidebar,
19+
} from "@/components/ui/sidebar";
1520
import Link from "next/link";
16-
import { toast } from "sonner";
1721
import AppAvatar from "./avatar";
1822

1923
export function NavUser({
@@ -26,40 +30,6 @@ export function NavUser({
2630
};
2731
}) {
2832
const { isMobile } = useSidebar();
29-
const logout = async () => {
30-
const loadingToast = toast.loading("正在登出……");
31-
32-
try {
33-
const res = await fetch("/api/auth/logout", {
34-
method: "POST",
35-
credentials: "include",
36-
});
37-
38-
if (res.ok) {
39-
toast.success("登出成功");
40-
// Redirect to login page with success message
41-
window.location.href = "/login?message=logged_out";
42-
return;
43-
}
44-
45-
const errorData = await res.json().catch(() => ({}));
46-
toast.error("登出失敗", {
47-
description: errorData.error_description || res.statusText,
48-
});
49-
50-
// Even if server logout fails, redirect to clear client state
51-
window.location.href = "/login?error=logout_failed";
52-
} catch (error) {
53-
toast.error("登出失敗", {
54-
description: error instanceof Error ? error.message : "未知錯誤",
55-
});
56-
57-
// Redirect to clear client state
58-
window.location.href = "/login?error=logout_failed";
59-
} finally {
60-
toast.dismiss(loadingToast);
61-
}
62-
};
6333

6434
return (
6535
<SidebarMenu>
@@ -120,10 +90,12 @@ export function NavUser({
12090
</Link>
12191
</DropdownMenuGroup>
12292
<DropdownMenuSeparator />
123-
<DropdownMenuItem onClick={logout}>
124-
<LogOut />
125-
登出
126-
</DropdownMenuItem>
93+
<Link href="/api/auth/logout">
94+
<DropdownMenuItem>
95+
<LogOut />
96+
登出
97+
</DropdownMenuItem>
98+
</Link>
12799
</DropdownMenuContent>
128100
</DropdownMenu>
129101
</SidebarMenuItem>

0 commit comments

Comments
 (0)