const CSRF_META_TAG_NAME = 'csrf-token';

/**
 * Get the CSRF token from the meta tag in the document head
 * The server should render: <meta name="csrf-token" content="..." />
 */
export function getCsrfToken(): string | null {
  if (typeof document === 'undefined') {
    return null;
  }

  const metaTag = document.querySelector<HTMLMetaElement>(
    `meta[name="${CSRF_META_TAG_NAME}"]`
  );

  return metaTag?.content || null;
}
