diff options
author | 2022-07-10 16:07:45 +0200 | |
---|---|---|
committer | 2022-07-11 17:47:53 +0200 | |
commit | fa1a7037b47f2e0114d8abc5a99d29239bd3637b (patch) | |
tree | 83404acf711aa8976dce47950edcca64836e0cd8 /src/api/utils | |
parent | 6.0.0-nightly.96 [skip ci] (diff) | |
download | ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.tar.gz ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.tar.zst ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.zip |
refactor: local server import/export
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'src/api/utils')
-rw-r--r-- | src/api/utils/auth.ts | 21 |
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 @@ | |||
1 | import localStorage from 'mobx-localstorage'; | 1 | import localStorage from 'mobx-localstorage'; |
2 | import { when } from 'mobx'; | ||
3 | import { localServerToken, needsToken } from '../apiBase'; | ||
2 | import { ferdiumLocale, ferdiumVersion } from '../../environment-remote'; | 4 | import { ferdiumLocale, ferdiumVersion } from '../../environment-remote'; |
3 | 5 | ||
4 | export const prepareAuthRequest = ( | 6 | export const prepareAuthRequest = ( |
@@ -29,10 +31,23 @@ export const prepareAuthRequest = ( | |||
29 | return request; | 31 | return request; |
30 | }; | 32 | }; |
31 | 33 | ||
32 | export const sendAuthRequest = ( | 34 | export 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 | |||
44 | export 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 | }; | ||