-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.js
69 lines (61 loc) · 2.11 KB
/
configuration.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
64
65
66
67
68
69
const hosts_local = {
arithmetic_servoce: "http://localhost:4000",
primes_service: "http://localhost:8085",
ciphers_service: "http://localhost:4006",
quotation_service: "http://localhost:4004"
};
const hosts_remote = {
arithmetic_servoce: "https://arithmetic-service.onrender.com",
primes_service: "https://primes-service.onrender.com",
ciphers_service: "https://ciphers-service-hbahramian.onrender.com",
quotation_service: "?"
};
const mode = 0;
function getHosts() {
return (mode == 0) ? hosts_local : hosts_remote;
}
let configuration = { loggedIn: false, hosts: getHosts(), token: "" };
function displayLogin() {
let loginDiv = document.getElementById("login");
if (configuration.loggedIn) {
loginDiv.innerHTML = `<h4>You are logged in.</h4>`;
} else {
loginDiv.innerHTML = `<form>
<input id="username" placeholder="Enter your username" required/>
<input id="password" placeholder="Enter your password" required/>
<button type="button" onclick="login()">Login</button>
</form>`;
}
}
async function login() {
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
let customer = { username: username, password: password }
let request = {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(customer)
};
try {
let response = await fetch(getHosts().primes_service + "/login", request);
if (response.status == 200) {
const token = await response.text();
configuration.token = token;
configuration.loggedIn = true;
displayLogin();
} else {
console.log(`response status:${response.status}`);
configuration.loggedIn = false;
displayLogin();
alert("Something went wrong!");
}
}
catch (error) {
console.log(error);
configuration.loggedIn = false;
displayLogin();
alert("Something went wrong!");
}
}