aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/utils/auth.js
blob: 4dbfcd6d4d12e31c5c2f72f6f6295bcdc0ea2875 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { remote } from 'electron';
import localStorage from 'mobx-localstorage';

const { app } = remote;

export const prepareAuthRequest = (options = { method: 'GET' }, auth = true) => {
  const request = Object.assign(options, {
    mode: 'cors',
    headers: Object.assign({
      'Content-Type': 'application/json',
      'X-Ferdi-Source': 'desktop',
      'X-Ferdi-Version': app.getVersion(),
      'X-Ferdi-platform': process.platform,
      'X-Ferdi-Timezone-Offset': new Date().getTimezoneOffset(),
      'X-Ferdi-System-Locale': app.getLocale(),
    }, options.headers),
  });

  if (auth) {
    request.headers.Authorization = `Bearer ${localStorage.getItem('authToken')}`;
  }

  return request;
};

export const sendAuthRequest = (url, options, auth) => (
  window.fetch(url, prepareAuthRequest(options, auth))
);