diff options
author | Vijay A <vraravam@users.noreply.github.com> | 2021-10-06 22:59:03 +0530 |
---|---|---|
committer | Vijay A <vraravam@users.noreply.github.com> | 2021-10-06 22:59:03 +0530 |
commit | 5fd7cd12cc62ceb6c4d654b3cb3b536412ed1216 (patch) | |
tree | 10cea6c608baea3481595ed9eb7be63ef03ca6c4 /src/api/utils/auth.ts | |
parent | Bumped up version to: 5.6.2 (hotfix) (diff) | |
parent | 5.6.3-nightly.25 [skip ci] (diff) | |
download | ferdium-app-5fd7cd12cc62ceb6c4d654b3cb3b536412ed1216.tar.gz ferdium-app-5fd7cd12cc62ceb6c4d654b3cb3b536412ed1216.tar.zst ferdium-app-5fd7cd12cc62ceb6c4d654b3cb3b536412ed1216.zip |
Merge branch 'nightly' into release
Diffstat (limited to 'src/api/utils/auth.ts')
-rw-r--r-- | src/api/utils/auth.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/api/utils/auth.ts b/src/api/utils/auth.ts new file mode 100644 index 000000000..98295d1a4 --- /dev/null +++ b/src/api/utils/auth.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | import localStorage from 'mobx-localstorage'; | ||
2 | import { ferdiLocale, ferdiVersion } from '../../environment-remote'; | ||
3 | |||
4 | export const prepareAuthRequest = ( | ||
5 | // eslint-disable-next-line unicorn/no-object-as-default-parameter | ||
6 | options = { method: 'GET' }, | ||
7 | auth = true, | ||
8 | ) => { | ||
9 | const request = Object.assign(options, { | ||
10 | mode: 'cors', | ||
11 | headers: { | ||
12 | 'Content-Type': 'application/json', | ||
13 | 'X-Franz-Source': 'desktop', | ||
14 | 'X-Franz-Version': ferdiVersion, | ||
15 | 'X-Franz-platform': process.platform, | ||
16 | 'X-Franz-Timezone-Offset': new Date().getTimezoneOffset(), | ||
17 | 'X-Franz-System-Locale': ferdiLocale, | ||
18 | // @ts-expect-error Property 'headers' does not exist on type '{ method: string; }'. | ||
19 | ...options.headers, | ||
20 | }, | ||
21 | }); | ||
22 | |||
23 | if (auth) { | ||
24 | request.headers.Authorization = `Bearer ${localStorage.getItem( | ||
25 | 'authToken', | ||
26 | )}`; | ||
27 | } | ||
28 | |||
29 | return request; | ||
30 | }; | ||
31 | |||
32 | export const sendAuthRequest = ( | ||
33 | url: RequestInfo, | ||
34 | options: { method: string } | undefined, | ||
35 | auth?: boolean, | ||
36 | ) => | ||
37 | // @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)); | ||