aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/utils/auth.ts
diff options
context:
space:
mode:
authorLibravatar Markus Hatvan <markus_hatvan@aon.at>2021-10-02 09:24:32 +0200
committerLibravatar GitHub <noreply@github.com>2021-10-02 09:24:32 +0200
commitbfe8847d72cd0893230f2e654242658214943e61 (patch)
tree3384b02ebad7a74cbb106ddd95546e0e24ff0bb8 /src/api/utils/auth.ts
parentfix: Fix navigation shortcut accelerator for non-macos (fixes #1172) (#2012) (diff)
downloadferdium-app-bfe8847d72cd0893230f2e654242658214943e61.tar.gz
ferdium-app-bfe8847d72cd0893230f2e654242658214943e61.tar.zst
ferdium-app-bfe8847d72cd0893230f2e654242658214943e61.zip
chore: convert various files from JS to TS (#2010)
Diffstat (limited to 'src/api/utils/auth.ts')
-rw-r--r--src/api/utils/auth.ts38
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 @@
1import localStorage from 'mobx-localstorage';
2import { ferdiLocale, ferdiVersion } from '../../environment-remote';
3
4export 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
32export 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));