LogoutButton.js 854 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // components/LogoutButton.js
  2. "use client";
  3. import { Button } from "@nextui-org/react";
  4. import { useRouter } from "next/navigation";
  5. import { useState } from "react";
  6. export default function LogoutButton() {
  7. const [isLoadingLogout, setIsLoadingLogout] = useState(false);
  8. const router = useRouter();
  9. const handleLogout = async () => {
  10. setIsLoadingLogout(true);
  11. try {
  12. const response = await fetch("/api/auth/logout", {
  13. method: "GET",
  14. });
  15. if (response.ok) {
  16. router.push("/logout");
  17. }
  18. } finally {
  19. setIsLoadingLogout(false);
  20. }
  21. };
  22. return (
  23. <Button
  24. color="primary"
  25. variant="faded"
  26. isLoading={isLoadingLogout}
  27. onPress={handleLogout}
  28. size="sm"
  29. className="text-black hover:bg-orange-300 hover:text-orange-800"
  30. radius="full"
  31. >
  32. {isLoadingLogout ? "Wird abgemeldet..." : "Logout"}
  33. </Button>
  34. );
  35. }