Skip to content

Commit

Permalink
fix: csrf token singleton
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Nov 4, 2024
1 parent 03c0ef0 commit b4e935f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions apps/renderer/src/lib/api-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ import { NeedActivationToast } from "~/modules/activation/NeedActivationToast"
import { DebugRegistry } from "~/modules/debug/registry"

let csrfTokenPromise: Promise<string> | null = null

const getPromisedCsrfToken = async () => {
if (!csrfTokenPromise) {
csrfTokenPromise = getCsrfToken()
}

return await csrfTokenPromise
}
export const apiFetch = ofetch.create({
baseURL: env.VITE_API_URL,
credentials: "include",
retry: false,
onRequest: async ({ options }) => {
if (!csrfTokenPromise) {
csrfTokenPromise = getCsrfToken()
}

const csrfToken = await csrfTokenPromise
const csrfToken = await getPromisedCsrfToken()

const header = new Headers(options.headers)

Expand Down Expand Up @@ -92,7 +96,7 @@ export const apiClient = hc<AppType>(env.VITE_API_URL, {
return {
"X-App-Version": PKG.version,
"X-App-Dev": process.env.NODE_ENV === "development" ? "1" : "0",
"X-Csrf-Token": await getCsrfToken(),
"X-Csrf-Token": await getPromisedCsrfToken(),
}
},
})
Expand Down

0 comments on commit b4e935f

Please sign in to comment.