F
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user