-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (34 loc) · 1.13 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
console.log("index.js loaded!");
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("login-form");
form.addEventListener("submit", async (event) => {
event.preventDefault();
const email = document.getElementById("floatingInput").value;
const password = document.getElementById("floatingPassword").value;
await login(email, password);
});
});
async function login(email, password) {
const credentials = email + ":" + password;
const encodedCredentials = btoa(credentials);
const headers = {
"Authorization": "Basic " + encodedCredentials,
"Content-Type": "application/json"
};
try {
const response = await fetch("https://learn.reboot01.com/api/auth/signin", {
method: "POST",
headers: headers
});
if (response.ok) {
const jwt = await response.text();
localStorage.setItem("jwt", jwt);
window.location.href = "profile.html";
} else {
displayAlert("Login failed: Invalid email or password.", "danger"); //still need to create this function :)
}
} catch (error) {
console.error("Error during login:", error);
alert("An error occurred. Please try again.");
}
}