21 lines · 659 B
import { useEffect, useRef } from "react";

const PARAM = "bootstrap";

/**
 * Reads the one-time `?bootstrap=` admin token and strips it from the address bar right away, so it doesn't
 * linger in history or reach page-view analytics.
 */
export function useBootstrapToken(): { current: string | undefined } {
  const token = useRef<string | undefined>(undefined);
  useEffect(() => {
    const url = new URL(window.location.href);
    const value = url.searchParams.get(PARAM);
    if (!value) return;
    token.current = value;
    url.searchParams.delete(PARAM);
    window.history.replaceState(window.history.state, "", url);
  }, []);
  return token;
}