-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathAPIUtils.js
51 lines (43 loc) · 1.27 KB
/
APIUtils.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { API_BASE_URL, ACCESS_TOKEN } from '../constants';
const request = (options) => {
const headers = new Headers({
'Content-Type': 'application/json',
})
if (localStorage.getItem(ACCESS_TOKEN)) {
headers.append('Authorization', 'Bearer ' + localStorage.getItem(ACCESS_TOKEN))
}
const defaults = { headers: headers };
options = Object.assign({}, defaults, options);
return fetch(options.url, options)
.then(response =>
response.json().then(json => {
if (!response.ok) {
return Promise.reject(json);
}
return json;
})
);
};
export function getCurrentUser() {
if (!localStorage.getItem(ACCESS_TOKEN)) {
return Promise.reject("No access token set.");
}
return request({
url: API_BASE_URL + "/api/auth/me",
method: 'GET'
});
}
export function login(loginRequest) {
return request({
url: API_BASE_URL + "/api/auth/login",
method: 'POST',
body: JSON.stringify(loginRequest)
});
}
export function signup(signupRequest) {
return request({
url: API_BASE_URL + "/api/auth/signup",
method: 'POST',
body: JSON.stringify(signupRequest)
});
}