Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

3.5 KB
Raw Blame
import { useEffect, type ReactNode } from "react";
import { AuthProvider } from "@editor/auth";
import { useAuth } from "@editor/auth/context";
import { Spinner } from "@editor/ui";
import { stripBasePath, withBasePath } from "@editor/constants/app";
import { rememberPendingDestination } from "@editor/services/pendingDestination";
import { ensureSaasSupabase } from "@processor/auth/saasSupabase";
import { EDITOR_URL } from "@processor/auth/editorUrl";

function FullScreen({ children }: { children: ReactNode }) {
  return (
    <div
      style={{
        minHeight: "100dvh",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
      }}
    >
      {children}
    </div>
  );
}

/**
 * SaaS portal gate: enter only with backend-granted portal/processor access
 * (`portalAccess`, from /api/v1/auth/me), mirroring self-hosted RequirePortalAccess.
 * The old "any signed-in account may enter" behaviour let team members without
 * access into the Processor.
 *
 * portalAccess resolves *after* the session does (/me runs once `loading` is
 * already false), so treat "real session, access not yet known" (raw
 * user.portalAccess still undefined, and not admin-by-role) as still-loading
 * rather than bouncing a legitimate user mid-load. Once settled: no session ->
 * login; a guest or a real account without access -> the free editor.
 */
function SaasPortalGate({ children }: { children: ReactNode }) {
  const { session, loading, isAnonymous, portalAccess, user } = useAuth();

  const accessPending =
    !!session &&
    !isAnonymous &&
    !portalAccess &&
    user?.portalAccess === undefined;
  const settling = loading || accessPending;

  const redirectTo = settling
    ? null
    : !session
      ? withBasePath("/login")
      : isAnonymous || !portalAccess
        ? EDITOR_URL
        : null;
  const bouncingToLogin = !settling && !session;

  useEffect(() => {
    if (!redirectTo) return;
    // A full page load, so the attempted path cannot ride along in router state.
    // Only on the sign-in bounce: an account without portal access is not one
    // sign-in away from this page.
    if (bouncingToLogin) {
      rememberPendingDestination(
        `${stripBasePath(window.location.pathname)}${window.location.search}${window.location.hash}`,
      );
    }
    window.location.href = redirectTo;
  }, [redirectTo, bouncingToLogin]);

  if (settling || redirectTo) {
    return (
      <FullScreen>
        <Spinner size="lg" />
      </FullScreen>
    );
  }
  return <>{children}</>;
}

/**
 * SaaS override of the portal auth boundary: authenticate against the SaaS Supabase
 * project (inheriting the editor's session) instead of the self-hosted Spring login.
 *
 * The saas session provider the saas-layer hooks read is mounted by the platform;
 * the gate reads the unified auth context, which only the unified AuthProvider
 * feeds, so that one is mounted here. Both read the same persisted session.
 */
export function PortalAuthBoundary({ children }: { children: ReactNode }) {
  // Configure the shared Supabase client (SaaS project) synchronously here, before
  // the AuthProvider below reads it — a useEffect would run too late for the first
  // render, leaving the provider with a null client. Idempotent; the portal mounts
  // outside the editor's AppProviders but against the SAME project, so a user already
  // signed into the editor is picked up from the persisted session (no second login).
  ensureSaasSupabase();
  return (
    <AuthProvider mode="supabase">
      <SaasPortalGate>{children}</SaasPortalGate>
    </AuthProvider>
  );
}