-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceCalls.js
59 lines (48 loc) · 1.51 KB
/
serviceCalls.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
function login(username, password) {
var tokenResponse = ""
var data = JSON.stringify({
"identifier": username,
"password": password
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
try {
tokenResponse = JSON.parse(xhr.responseText);
}
catch (err) {
tokenResponse = {
message: "Something went wrong, could not log in."
}
}
}
}
xhr.open("POST", "http://devel.uniqcast.com:3001/auth/local", false);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(data);
return tokenResponse;
}
function fetchChannels(token) {
var channels = "";
var data = "";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
try {
channels = JSON.parse(xhr.responseText);
}
catch (err) {
channels = {
message: "Something went wrong, could not load the channels."
}
}
}
}
xhr.open("GET", "http://devel.uniqcast.com:3001/channels", false);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer " + token);
xhr.send(data);
return channels;
}