mirror of
https://github.com/outline/outline.git
synced 2026-06-13 11:25:03 +03:00
0a9bd39aac
ref OUT-Q325-03
31 lines
718 B
TypeScript
31 lines
718 B
TypeScript
import { CSRF } from "@shared/constants";
|
|
import { useState, useEffect } from "react";
|
|
import { getCookie } from "tiny-cookie";
|
|
|
|
/**
|
|
* React hook for accessing CSRF tokens in components
|
|
*
|
|
* @returns The CSRF token string or null if not found
|
|
*/
|
|
export function useCsrfToken() {
|
|
const [token, setToken] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const updateToken = () => {
|
|
const currentToken = getCookie(CSRF.cookieName);
|
|
|
|
setToken(currentToken);
|
|
};
|
|
|
|
// Initial load
|
|
updateToken();
|
|
|
|
// Listen for cookie changes (when navigating or refreshing)
|
|
const interval = setInterval(updateToken, 1000);
|
|
|
|
return () => clearInterval(interval);
|
|
}, []);
|
|
|
|
return token;
|
|
}
|