-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (56 loc) · 1.56 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
const API_URL = "http://localhost:8888";
const AUTH_URL = "http://localhost:3000";
let ACCESS_TOKEN = undefined;
const headlineBtn = document.querySelector('#headline');
const secretBtn = document.querySelector("#secret");
const loginBtn = document.querySelector('#loginBtn');
const logoutBtn = document.querySelector('#logoutBtn');
headlineBtn.addEventListener('click', () => {
fetch(`${API_URL}/resource`).then(resp => {
return resp.text();
}).then( data => {
UIUpdate.alertBox(data);
});
});
secretBtn.addEventListener('click', (event) => {
let headers = {};
if(ACCESS_TOKEN) {
headers = {
'Authorization': `Bearer ${ACCESS_TOKEN}`
};
}
fetch(`${API_URL}/resource/secret`, { headers }).then(resp => {
UIUpdate.updateCat(resp.status);
return resp.text();
}).then( data => {
UIUpdate.alertBox(data);
});
});
logoutBtn.addEventListener('click', (event) => {
ACCESS_TOKEN = undefined;
UIUpdate.loggedOut();
});
loginBtn.addEventListener('click', (event) => {
fetch(`${AUTH_URL}/login`, {
method: 'POST',
headers: {
'Content-type': 'application/json',
'accept': 'application/json',
},
body: JSON.stringify(UIUpdate.getUsernamePassword())
}).then( resp => {
UIUpdate.updateCat(resp.status);
if(resp.status == 200) {
return resp.json();
} else {
return resp.text();
}
}).then(data => {
if(data.access_token) {
ACCESS_TOKEN = data.access_token;
data = `Access Token: ${data.access_token}`;
UIUpdate.loggedIn();
}
UIUpdate.alertBox(data);
});
});