diff --git a/src/lib/csrf/client.ts b/src/lib/csrf/client.ts new file mode 100644 index 0000000..49c0c03 --- /dev/null +++ b/src/lib/csrf/client.ts @@ -0,0 +1,17 @@ +const CSRF_META_TAG_NAME = 'csrf-token'; + +/** + * Get the CSRF token from the meta tag in the document head + * The server should render: + */ +export function getCsrfToken(): string | null { + if (typeof document === 'undefined') { + return null; + } + + const metaTag = document.querySelector( + `meta[name="${CSRF_META_TAG_NAME}"]` + ); + + return metaTag?.content || null; +}