-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (131 loc) · 4.04 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
console.log("at app.js");
const letRedirect = async (url) => {
await fetch(url, { method: "GET" })
.then((response) => {
if (response.redirected) {
window.location.href = response.url;
}
})
.catch((error) => {
console.log("ERR: Something is wrong. Please refresh the page");
});
};
const setNavBar = async () => {
const nav = document.getElementById("navElem");
// if user is authenticated:
try {
let res = await fetch("/isloggedin");
console.log("at isloggedin", res);
let jsonRes = await res.json();
if (res.status === 200 && jsonRes.isLoggedIn === true) {
// logged in
let email = jsonRes.userCookie.email;
let userName;
if (!email.includes("@")) {
userName = email;
if (email === "admin") {
const adminLine = document.createElement("li");
adminLine.innerHTML = '<a href="./admin">Admin</a>';
nav.appendChild(adminLine);
}
} else {
userName = email.substring(0, email.indexOf("@"));
}
console.log("from app.js:", userName);
const cartLine = document.createElement("li");
cartLine.innerHTML = '<a href="./cart.html">Cart</a>';
nav.appendChild(cartLine);
const logoutLine = document.createElement("li");
logoutLine.innerHTML = '<a href="/logout">Logout</a>';
const nameElement = document.createElement("li");
nameElement.innerText = `Hello, ${userName}!`;
nameElement.style.color = "white";
nav.appendChild(logoutLine);
nav.appendChild(nameElement);
} else {
// not logged in
const loginLine = document.createElement("li");
loginLine.innerHTML = '<a href="./login.html">Log In</a>';
nav.appendChild(loginLine);
}
} catch (error) {
console.log("ERR: ", error);
window.alert("Something went wrong.. Sorry");
setTimeout(() => {
letRedirect("/notSuccessLogin");
}, 1000);
}
};
const onClickLoginEventHandler = async () => {
const email = document.getElementById("Uname").value;
const password = document.getElementById("Pass").value;
const rememberMe = document.getElementById("check").checked;
try {
let res = await fetch(`/login`, {
body: JSON.stringify({
email: email,
password: password,
rememberMe: rememberMe,
}),
cache: "no-cache",
method: "POST",
headers: { "Content-Type": "application/json" },
});
if (!res) {
throw new Error("No result about this user");
}
if (res.status !== 200) {
throw new Error(res.message);
}
res = await res.json();
if (res.emailExists && res.passwordExists) {
letRedirect(`/successLogin`);
}
if (res.emailExists && !res.passwordExists) {
window.alert("Wrong password. Please try again.");
setTimeout(() => {
letRedirect("/notSuccessLogin");
}, 1000);
}
} catch (error) {
console.log("errr ", error);
window.alert("Login failed. Please try again");
setTimeout(() => {
letRedirect("/notSuccessLogin");
}, 1000);
}
};
const onClickRegisterEventHandler = async () => {
const email = document.getElementById("Uname").value;
const password = document.getElementById("Pass").value;
try {
let res = await fetch(`/register`, {
body: JSON.stringify({
email: email,
password: password,
}),
cache: "no-cache",
method: "POST",
headers: { "Content-Type": "application/json" },
});
res = await res.json();
if (res.emailExists) {
window.alert("Sorry, that email is taken. Maybe you need to login");
setTimeout(() => {
letRedirect(`/notSuccessLogin`);
}, 1000);
}
if (res.newUserWasAdded === true) {
window.alert("New user was created. Please login");
setTimeout(() => {
letRedirect(`/successLogin`);
}, 1000);
}
} catch (error) {
console.log("ERR ", error);
window.alert("Register failed. Please try again");
setTimeout(() => {
letRedirect("/redirectHome");
}, 1000);
}
};