-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
77 lines (73 loc) · 2.72 KB
/
login.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
70
71
72
73
74
75
76
77
loginTab = document.getElementById("login-tab");
registerTab = document.getElementById("register-tab");
loginForm = document.getElementById("login-form");
registerForm = document.getElementById("register-form");
loginTab.addEventListener("click", function () {
loginForm.classList.remove("hidden");
registerForm.classList.add("hidden");
this.classList.add("bg-blue-500", "text-white");
registerTab.classList.remove("bg-blue-500", "text-white");
registerTab.classList.add("text-blue-500");
});
loginForm.addEventListener("submit", function (e) {
e.preventDefault();
let email = document.getElementById("login-email").value;
let password = document.getElementById("login-password").value;
let data = {
email: email,
password: password,
};
apiCall("/user/login", "POST", data, (res) => {
userData = res;
sessionStorage.setItem("user", JSON.stringify(userData));
// userCookie = JSON.parse(document.cookie.split("; ").find(row => row.startsWith("user=")).split("=")[1]);
window.location.href = "products.html";
});
});
registerTab.addEventListener("click", function () {
registerForm.classList.remove("hidden");
loginForm.classList.add("hidden");
this.classList.add("bg-blue-500", "text-white");
loginTab.classList.remove("bg-blue-500", "text-white");
loginTab.classList.add("text-blue-500");
});
registerForm.addEventListener("submit", function (e) {
e.preventDefault();
let firstName = document.getElementById("register-firstName").value;
let lastName = document.getElementById("register-lastName").value;
let email = document.getElementById("register-email").value;
let password = document.getElementById("register-password").value;
let confirm_password = document.getElementById("register-confirm-password").value;
if(password != confirm_password){
alert("Passwords do not match.");
return;
}
let data = {
firstName : firstName,
lastName : lastName,
email: email,
password: password,
};
// fetch(BACKEND_API_URL+"/user/create", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify(data),
// })
// .then((res) => {
// if (res.status === 200) {
// window.location.href = "/dashboard";
// } else {
// alert("Error creating user.");
// }
// })
// .catch((error) => {
// console.error(error);
// });
apiCall("/user/create", "POST", data, (res) => {
userData = res;
sessionStorage.setItem("user", JSON.stringify(userData));
window.location.href = "products.html";
});
});