This commit is contained in:
2026-05-22 22:14:15 +03:00
parent e9d1b733a5
commit fac5e2ea5e
7 changed files with 28 additions and 30 deletions

View File

@@ -6,17 +6,28 @@ interface CsrfResponse {
}
let cachedToken: string | null = null
let inflight: Promise<string> | null = null
export function clearCsrfCache(): void {
cachedToken = null
inflight = null
}
export async function getCsrfToken(): Promise<string> {
if (cachedToken) return cachedToken
const res = await fetch(`${API_URL}/csrf/token`, {
credentials: 'include',
})
const data: CsrfResponse = await res.json()
cachedToken = data.token
return cachedToken
export function getCsrfToken(): Promise<string> {
if (cachedToken) return Promise.resolve(cachedToken)
if (inflight) return inflight
inflight = fetch(`${API_URL}/csrf/token`, { credentials: 'include' })
.then((res) => res.json() as Promise<CsrfResponse>)
.then((data) => {
cachedToken = data.token
inflight = null
return cachedToken
})
.catch((err) => {
inflight = null
throw err
})
return inflight
}