Skip to content

Commit 16b5349

Browse files
authored
fix: csrf token singleton (#1463)
Signed-off-by: Innei <tukon479@gmail.com>
1 parent e31a611 commit 16b5349

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

apps/renderer/src/lib/api-fetch.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ import { NeedActivationToast } from "~/modules/activation/NeedActivationToast"
1414
import { DebugRegistry } from "~/modules/debug/registry"
1515

1616
let csrfTokenPromise: Promise<string> | null = null
17+
18+
const getPromisedCsrfToken = async () => {
19+
if (!csrfTokenPromise) {
20+
csrfTokenPromise = getCsrfToken()
21+
}
22+
23+
return await csrfTokenPromise
24+
}
1725
export const apiFetch = ofetch.create({
1826
baseURL: env.VITE_API_URL,
1927
credentials: "include",
2028
retry: false,
2129
onRequest: async ({ options }) => {
22-
if (!csrfTokenPromise) {
23-
csrfTokenPromise = getCsrfToken()
24-
}
25-
26-
const csrfToken = await csrfTokenPromise
30+
const csrfToken = await getPromisedCsrfToken()
2731

2832
const header = new Headers(options.headers)
2933

@@ -92,7 +96,7 @@ export const apiClient = hc<AppType>(env.VITE_API_URL, {
9296
return {
9397
"X-App-Version": PKG.version,
9498
"X-App-Dev": process.env.NODE_ENV === "development" ? "1" : "0",
95-
"X-Csrf-Token": await getCsrfToken(),
99+
"X-Csrf-Token": await getPromisedCsrfToken(),
96100
}
97101
},
98102
})

0 commit comments

Comments
 (0)