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.
18 lines
320 B
TypeScript
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') ?? '';
|
|
}
|