Skip to content

Commit f642ace

Browse files
committed
Implement logout
1 parent 9b430ae commit f642ace

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

web/src/sections/AppSidebar/Settings.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import SvgSettings from "@/icons/settings";
2222
import SvgLogOut from "@/icons/log-out";
2323
import SvgBell from "@/icons/bell";
2424
import SvgX from "@/icons/x";
25+
import { useRouter } from "next/navigation";
2526

2627
function getUsernameFromEmail(email?: string): string {
2728
if (!email) return ANONYMOUS_USER_NAME;
@@ -49,12 +50,26 @@ export default function Settings({ folded }: SettingsProps) {
4950
error,
5051
mutate: refreshNotifications,
5152
} = useSWR<Notification[]>("/api/notifications", errorHandlingFetcher);
53+
const router = useRouter();
5254

5355
const showAdminPanel = !user || user.role === UserRole.ADMIN;
5456
const showCuratorPanel = user && isCurator;
5557
const showLogout =
5658
user && !checkUserIsNoAuthUser(user.id) && !LOGOUT_DISABLED;
5759

60+
async function handleLogout() {
61+
const isSuccess = await logout();
62+
63+
if (!isSuccess) {
64+
alert("Failed to logout");
65+
return;
66+
}
67+
68+
router.push(
69+
`/auth/login?next=${encodeURIComponent(window.location.pathname + window.location.search)}`
70+
);
71+
}
72+
5873
return (
5974
<>
6075
<Popover open={settingsPopupOpen} onOpenChange={setSettingsPopupOpen}>
@@ -132,14 +147,14 @@ export default function Settings({ folded }: SettingsProps) {
132147
{`Notifications ${(notifications && notifications.length) || 0 > 0 ? `(${notifications!.length})` : ""}`}
133148
</MenuButton>
134149

135-
{showLogout && (
150+
{!showLogout && (
136151
<>
137152
{(showCuratorPanel ||
138153
showAdminPanel ||
139154
dropdownItems.length > 0) && (
140155
<div className="border-b mx-padding-button" />
141156
)}
142-
<MenuButton icon={SvgLogOut} danger>
157+
<MenuButton icon={SvgLogOut} danger onClick={handleLogout}>
143158
Log out
144159
</MenuButton>
145160
</>

0 commit comments

Comments
 (0)