Files
myeasycms-v2/packages/shared/src/hooks/use-csrf-token.ts
giancarlo e4d4069aa8 Add check for document in useCsrfToken hook
This commit introduces a check for the existence of 'document' in 'useCsrfToken' hook. This is to protect against errors when using this hook in a non-browser environment where 'document' is not defined.
2024-05-20 12:12:07 +07:00

18 lines
320 B
TypeScript

/**
* Get the CSRF token from the meta tag.
* @returns The CSRF token.
*/
export function useCsrfToken() {
if (typeof document === 'undefined') {
return '';
}
const meta = document.querySelector('meta[name="csrf-token"]');
if (!meta) {
return '';
}
return meta.getAttribute('content') ?? '';
}