aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/utils/auth.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/utils/auth.ts')
-rw-r--r--src/api/utils/auth.ts21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/api/utils/auth.ts b/src/api/utils/auth.ts
index a7a73309d..282d00459 100644
--- a/src/api/utils/auth.ts
+++ b/src/api/utils/auth.ts
@@ -1,4 +1,6 @@
1import localStorage from 'mobx-localstorage'; 1import localStorage from 'mobx-localstorage';
2import { when } from 'mobx';
3import { localServerToken, needsToken } from '../apiBase';
2import { ferdiumLocale, ferdiumVersion } from '../../environment-remote'; 4import { ferdiumLocale, ferdiumVersion } from '../../environment-remote';
3 5
4export const prepareAuthRequest = ( 6export const prepareAuthRequest = (
@@ -29,10 +31,23 @@ export const prepareAuthRequest = (
29 return request; 31 return request;
30}; 32};
31 33
32export const sendAuthRequest = ( 34export const prepareLocalToken = async (
35 requestData: { method: string; headers?: any; body?: any },
36) => {
37 await when(() => !needsToken() || !!localServerToken(), { timeout: 2000 });
38 const token = localServerToken();
39 if (token) {
40 requestData.headers['X-Ferdium-Local-Token'] = token;
41 }
42}
43
44export const sendAuthRequest = async (
33 url: RequestInfo, 45 url: RequestInfo,
34 options?: { method: string; headers?: any; body?: any }, 46 options?: { method: string; headers?: any; body?: any },
35 auth?: boolean, 47 auth?: boolean,
36) => 48) => {
49 const request = prepareAuthRequest(options, auth);
50 await prepareLocalToken(request);
37 // @ts-expect-error Argument of type '{ method: string; } & { mode: string; headers: any; }' is not assignable to parameter of type 'RequestInit | undefined'. 51 // @ts-expect-error Argument of type '{ method: string; } & { mode: string; headers: any; }' is not assignable to parameter of type 'RequestInit | undefined'.
38 window.fetch(url, prepareAuthRequest(options, auth)); 52 return window.fetch(url, request);
53};