Skip to content

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

1.7 KB
Raw Blame
import type { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import type { TFunction } from "i18next";
import {
  SpringAuthProvider,
  deriveDisplayName as deriveDisplayNameShared,
} from "@editor/auth/spring/UseSession";
import { useAuth as useSharedAuth } from "@editor/auth/context";
import type { AuthUser } from "@editor/auth/types";
// Side-effect import: wires the editor's transport + platform seams into the
// shared Spring engine before PlatformProviders mounts the provider below.
import "@editor/auth/configureSpringAuth";

export type { AuthUser as User } from "@editor/auth/types";

/**
 * Editor display-name helper. Keeps the i18next `TFunction` signature the
 * editor's components and tests rely on, delegating to the shared
 * implementation for the actual logic (anonymous placeholder vs username/email).
 */
export function deriveDisplayName(
  user: AuthUser | null | undefined,
  t: TFunction,
): string | null {
  return deriveDisplayNameShared(user, (key, fallback) => t(key, fallback));
}

/**
 * Auth Provider for the editor. Wraps the shared Spring provider and feeds it an
 * i18next-backed translate function so the localised "User" placeholder still
 * works for anonymous sessions.
 */
export function AuthProvider({ children }: { children: ReactNode }) {
  const { t } = useTranslation();
  return (
    <SpringAuthProvider translate={(key, fallback) => t(key, fallback)}>
      {children}
    </SpringAuthProvider>
  );
}

/** Hook to access auth context. Must be used within AuthProvider. */
export function useAuth() {
  return useSharedAuth();
}

/** Debug alias kept for backwards compatibility with existing callers. */
export function useAuthDebug() {
  return useSharedAuth();
}