58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { LogOut } from "lucide-react";
|
|
|
|
import { getApiUrl } from "@/utils/api";
|
|
import { MixpanelEvent, trackEvent } from "@/utils/mixpanel";
|
|
|
|
type LogoutButtonProps = {
|
|
label?: string;
|
|
className?: string;
|
|
iconOnly?: boolean;
|
|
};
|
|
|
|
export default function LogoutButton({
|
|
label = "Logout",
|
|
className = "",
|
|
iconOnly = false,
|
|
}: LogoutButtonProps) {
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const handleLogout = async () => {
|
|
if (isSubmitting) {
|
|
return;
|
|
}
|
|
|
|
setIsSubmitting(true);
|
|
trackEvent(MixpanelEvent.Auth_Signed_Out, {
|
|
source: "logout_button",
|
|
});
|
|
try {
|
|
await fetch(getApiUrl("/api/v1/auth/logout"), {
|
|
method: "POST",
|
|
credentials: "include",
|
|
});
|
|
} catch {
|
|
// Always route back to auth gate even if backend logout fails.
|
|
} finally {
|
|
window.location.replace("/");
|
|
setIsSubmitting(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={handleLogout}
|
|
disabled={isSubmitting}
|
|
className={className}
|
|
aria-label={label}
|
|
title={label}
|
|
>
|
|
<LogOut className="h-4 w-4" />
|
|
{!iconOnly ? <span>{isSubmitting ? "Signing out..." : label}</span> : null}
|
|
</button>
|
|
);
|
|
}
|