Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

2.4 KB
Raw Blame
import * as Sentry from "@sentry/react";
import {
  createContext,
  type PropsWithChildren,
  useEffect,
  useRef,
} from "react";
import { authClient } from "@/lib/auth-client";
import { descriptionSaveQueue } from "@/lib/description-save-queue";
import type { User } from "@/types/user";
import { BootFrame } from "../../common/boot-frame";

const { useSession } = authClient;

export const AuthContext = createContext<{
  user: User | null | undefined;
  isLoading: boolean;
  refetchUser: () => Promise<void>;
}>({
  user: undefined,
  isLoading: true,
  refetchUser: async () => {},
});

function AuthProvider({ children }: PropsWithChildren) {
  const { data, isPending, refetch, error } = useSession();
  // Only hold the tree back during the *first* session fetch. Better
  // Auth re-fetches the session on window focus; if we kept returning
  // nothing while those background fetches are pending we'd unmount the
  // entire route tree on every alt-tab, which tore down the Turnstile
  // iframe and forced a re-challenge.
  const previousOwner = useRef<string | null | undefined>(undefined);
  useEffect(() => {
    if (isPending || error) return;
    const owner = data?.user.id ?? null;
    if (previousOwner.current !== owner) descriptionSaveQueue.clear();
    previousOwner.current = owner;
  }, [data?.user.id, isPending, error]);
  const hasLoadedOnce = useRef(false);
  if (!isPending) {
    hasLoadedOnce.current = true;
  }

  // Tag transient Safari "Load failed" errors during the session fetch so
  // instrument.ts's beforeSend can drop them. Better Auth's useSession
  // uses nanostores rather than TanStack Query, so the query client's
  // network-error cooldown doesn't reach this path. Real auth failures
  // (e.g. 401) still surface to Sentry through Better Auth.
  useEffect(() => {
    if (error instanceof Error && error.message.includes("Load failed")) {
      Sentry.captureException(error, { tags: { area: "auth.session" } });
    }
  }, [error]);

  if (isPending && !hasLoadedOnce.current) {
    return <BootFrame />;
  }

  return (
    <AuthContext.Provider
      value={{
        user: (data?.user as User | null | undefined) ?? null,
        isLoading: isPending,
        refetchUser: async () => {
          await refetch({ query: { disableCookieCache: true } });
        },
      }}
    >
      {children}
    </AuthContext.Provider>
  );
}

export default AuthProvider;