SPB Git forge

spb/spinza

Public
8commits 1branches 0releases
1.6 MBsize
maindefault branch
16 days agolast push
TypeScript 97.6% SQL 1.4% JavaScript 0.5%
1.1 KB · 36 lines tsx
Raw Blame History
1"use client";23import { useEffect } from "react";4import { usePathname, useRouter } from "next/navigation";5import { useSession } from "@/lib/store";6import { Skeleton } from "@/components/ui";78/**9 * Gate for player-only screens. Guests are sent to /login?next=<current path>;10 * while the session is still hydrating, a skeleton keeps the layout stable.11 */12export function RequireAuth({ children, fallback }: { children: React.ReactNode; fallback?: React.ReactNode }) {13  const status = useSession((s) => s.status);14  const router = useRouter();15  const pathname = usePathname();1617  useEffect(() => {18    if (status === "guest") router.replace(`/login?next=${encodeURIComponent(pathname)}`);19  }, [status, router, pathname]);2021  if (status !== "authenticated") {22    return (23      <>24        {fallback ?? (25          <div className="space-y-4" aria-busy>26            <Skeleton className="h-8 w-48" />27            <Skeleton className="h-40 w-full rounded-lg" />28            <Skeleton className="h-24 w-full rounded-lg" />29          </div>30        )}31      </>32    );33  }34  return <>{children}</>;35}36