AuthGate.jsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use client";
  2. import React from "react";
  3. import { RefreshCw } from "lucide-react";
  4. import { useAuth } from "@/components/auth/authContext";
  5. import { Button } from "@/components/ui/button";
  6. import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
  7. import {
  8. Card,
  9. CardHeader,
  10. CardTitle,
  11. CardDescription,
  12. CardContent,
  13. CardFooter,
  14. } from "@/components/ui/card";
  15. export default function AuthGate({ children }) {
  16. const { status, error, retry } = useAuth();
  17. const canRetry = typeof retry === "function";
  18. if (status === "authenticated") {
  19. return children;
  20. }
  21. if (status === "error") {
  22. return (
  23. <Card>
  24. <CardHeader>
  25. <CardTitle>Sitzungsprüfung fehlgeschlagen</CardTitle>
  26. <CardDescription>
  27. Die Sitzung konnte nicht geprüft werden.
  28. </CardDescription>
  29. </CardHeader>
  30. <CardContent className="space-y-3">
  31. <Alert variant="destructive">
  32. <AlertTitle>Fehler</AlertTitle>
  33. <AlertDescription>
  34. {error ||
  35. "Bitte prüfen Sie Ihre Verbindung und versuchen Sie es erneut."}
  36. </AlertDescription>
  37. </Alert>
  38. </CardContent>
  39. <CardFooter className="flex flex-col gap-2 sm:flex-row sm:justify-end">
  40. <Button
  41. type="button"
  42. variant="outline"
  43. onClick={() => {
  44. if (canRetry) retry();
  45. else window.location.reload();
  46. }}
  47. >
  48. <RefreshCw className="h-4 w-4" />
  49. Erneut versuchen
  50. </Button>
  51. </CardFooter>
  52. </Card>
  53. );
  54. }
  55. // "unauthenticated" -> redirect happens in AuthProvider.
  56. // Keeping this message is fine because TopNav indicator is not shown in this state.
  57. if (status === "unauthenticated") {
  58. return (
  59. <Card>
  60. <CardHeader>
  61. <CardTitle>Weiterleitung</CardTitle>
  62. <CardDescription>
  63. Sie werden zum Login weitergeleitet.
  64. </CardDescription>
  65. </CardHeader>
  66. <CardContent>
  67. <p className="text-sm text-muted-foreground">Bitte warten…</p>
  68. </CardContent>
  69. </Card>
  70. );
  71. }
  72. // Default: loading (or unknown)
  73. // RHL-032:
  74. // Do not render a second "session checking" UI here.
  75. // The TopNav SessionIndicator is the single source of feedback.
  76. return null;
  77. }