Skip to content

Commit

Permalink
🐛 Fix logout menu item (#1479)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella authored Jan 3, 2025
1 parent 00548ab commit 2b90004
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
10 changes: 4 additions & 6 deletions apps/web/src/app/components/logout-button.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
"use client";
import { usePostHog } from "@rallly/posthog/client";
import type { ButtonProps } from "@rallly/ui/button";
import { Button } from "@rallly/ui/button";

import { useUser } from "@/components/user-provider";

export function LogoutButton({
children,
onClick,
...rest
}: React.PropsWithChildren<ButtonProps>) {
const posthog = usePostHog();
const { logout } = useUser();
return (
<Button
{...rest}
onClick={async (e) => {
await logout();
onClick?.(e);
await fetch("/api/logout", { method: "POST" });
posthog?.capture("logout");
posthog?.reset();
window.location.href = "/login";
}}
>
{children}
Expand Down
19 changes: 1 addition & 18 deletions apps/web/src/components/user-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,8 @@ import { isFeedbackEnabled } from "@/utils/constants";

import { IfAuthenticated, IfGuest, useUser } from "./user-provider";

function logout() {
// programmtically submit form with name="logout"
const form = document.forms.namedItem("logout");
if (form && typeof form.submit === "function") {
form.submit();
} else {
console.error("Logout form or submit method not found");
}
}

export const UserDropdown = ({ className }: { className?: string }) => {
const { user } = useUser();
const { user, logout } = useUser();
usePlan(); // prefetch plan data
return (
<DropdownMenu modal={false}>
Expand Down Expand Up @@ -177,13 +167,6 @@ export const UserDropdown = ({ className }: { className?: string }) => {
}}
className="flex items-center gap-x-2"
>
<form
className="hidden"
action="/auth/logout"
name="logout"
method="POST"
/>
{/* Don't use signOut() from next-auth. It doesn't work in vercel-production env. I don't know why. */}
<LogOutIcon className="text-muted-foreground size-4" />
<Trans i18nKey="logout" />
</DropdownMenuItem>
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/components/user-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const UserContext = React.createContext<{
userId?: string | null;
guestId?: string | null;
}) => boolean;
logout: () => Promise<void>;
} | null>(null);

export const useUser = () => {
Expand Down Expand Up @@ -105,6 +106,12 @@ export const UserProvider = (props: { children?: React.ReactNode }) => {
locale: user.locale ?? i18n.language,
},
refresh: session.update,
logout: async () => {
await fetch("/api/logout", { method: "POST" });
posthog?.capture("logout");
posthog?.reset();
window.location.href = "/login";
},
ownsObject: (resource) => {
return isOwner(resource, { id: user.id, isGuest });
},
Expand Down

0 comments on commit 2b90004

Please sign in to comment.