mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-02 11:46:03 +02:00
🐛 Fix logut menu item
This commit is contained in:
parent
00548ab763
commit
471d98d50b
3 changed files with 12 additions and 24 deletions
|
@ -1,23 +1,21 @@
|
||||||
"use client";
|
"use client";
|
||||||
import { usePostHog } from "@rallly/posthog/client";
|
|
||||||
import type { ButtonProps } from "@rallly/ui/button";
|
import type { ButtonProps } from "@rallly/ui/button";
|
||||||
import { Button } from "@rallly/ui/button";
|
import { Button } from "@rallly/ui/button";
|
||||||
|
|
||||||
|
import { useUser } from "@/components/user-provider";
|
||||||
|
|
||||||
export function LogoutButton({
|
export function LogoutButton({
|
||||||
children,
|
children,
|
||||||
onClick,
|
onClick,
|
||||||
...rest
|
...rest
|
||||||
}: React.PropsWithChildren<ButtonProps>) {
|
}: React.PropsWithChildren<ButtonProps>) {
|
||||||
const posthog = usePostHog();
|
const { logout } = useUser();
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
{...rest}
|
{...rest}
|
||||||
onClick={async (e) => {
|
onClick={async (e) => {
|
||||||
|
await logout();
|
||||||
onClick?.(e);
|
onClick?.(e);
|
||||||
await fetch("/api/logout", { method: "POST" });
|
|
||||||
posthog?.capture("logout");
|
|
||||||
posthog?.reset();
|
|
||||||
window.location.href = "/login";
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
@ -36,18 +36,8 @@ import { isFeedbackEnabled } from "@/utils/constants";
|
||||||
|
|
||||||
import { IfAuthenticated, IfGuest, useUser } from "./user-provider";
|
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 }) => {
|
export const UserDropdown = ({ className }: { className?: string }) => {
|
||||||
const { user } = useUser();
|
const { user, logout } = useUser();
|
||||||
usePlan(); // prefetch plan data
|
usePlan(); // prefetch plan data
|
||||||
return (
|
return (
|
||||||
<DropdownMenu modal={false}>
|
<DropdownMenu modal={false}>
|
||||||
|
@ -177,13 +167,6 @@ export const UserDropdown = ({ className }: { className?: string }) => {
|
||||||
}}
|
}}
|
||||||
className="flex items-center gap-x-2"
|
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" />
|
<LogOutIcon className="text-muted-foreground size-4" />
|
||||||
<Trans i18nKey="logout" />
|
<Trans i18nKey="logout" />
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
|
|
@ -33,6 +33,7 @@ export const UserContext = React.createContext<{
|
||||||
userId?: string | null;
|
userId?: string | null;
|
||||||
guestId?: string | null;
|
guestId?: string | null;
|
||||||
}) => boolean;
|
}) => boolean;
|
||||||
|
logout: () => Promise<void>;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
|
|
||||||
export const useUser = () => {
|
export const useUser = () => {
|
||||||
|
@ -105,6 +106,12 @@ export const UserProvider = (props: { children?: React.ReactNode }) => {
|
||||||
locale: user.locale ?? i18n.language,
|
locale: user.locale ?? i18n.language,
|
||||||
},
|
},
|
||||||
refresh: session.update,
|
refresh: session.update,
|
||||||
|
logout: async () => {
|
||||||
|
await fetch("/api/logout", { method: "POST" });
|
||||||
|
posthog?.capture("logout");
|
||||||
|
posthog?.reset();
|
||||||
|
window.location.href = "/login";
|
||||||
|
},
|
||||||
ownsObject: (resource) => {
|
ownsObject: (resource) => {
|
||||||
return isOwner(resource, { id: user.id, isGuest });
|
return isOwner(resource, { id: user.id, isGuest });
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue