Skip to content

Commit 5d011eb

Browse files
committed
Add: issue regarding signup and signin
1 parent cf6a979 commit 5d011eb

4 files changed

Lines changed: 38 additions & 21 deletions

File tree

auth.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ if (signupForm) {
2828
signupForm.addEventListener("submit", (event) => {
2929
event.preventDefault();
3030

31-
const username = document.getElementById("username").value;
32-
const email = document.getElementById("email").value;
31+
const username = document.getElementById("username").value.trim();
32+
const email = document.getElementById("email").value.trim();
3333
const password = document.getElementById("password").value;
3434
const confirmPassword = document.getElementById("confirmPassword").value;
3535
const error = document.getElementById("signupError");
@@ -41,10 +41,25 @@ if (signupForm) {
4141
return;
4242
}
4343

44-
const user = { username, email, password };
45-
localStorage.setItem("user", JSON.stringify(user));
44+
// getting existing users
45+
let users = JSON.parse(localStorage.getItem("users")) || [];
4646

47-
window.location.href = getMainPagePath("signin.html");
47+
// if username already exists
48+
const userExists = users.some(user => user.email === email);
49+
50+
if (userExists) {
51+
error.textContent = "User already exists!";
52+
return;
53+
}
54+
55+
// new user
56+
const newUser = { username, email, password };
57+
users.push(newUser);
58+
localStorage.setItem("users", JSON.stringify(users));
59+
60+
localStorage.setItem("loggedIn", "true");
61+
localStorage.setItem("loggedInUser", username);
62+
window.location.href = getMainPagePath("index.html");
4863
});
4964
}
5065

@@ -55,18 +70,19 @@ if (signinForm) {
5570
signinForm.addEventListener("submit", (event) => {
5671
event.preventDefault();
5772

58-
const email = document.getElementById("loginEmail").value;
73+
const email = document.getElementById("loginEmail").value.trim();
5974
const password = document.getElementById("loginPassword").value;
6075
const error = document.getElementById("loginError");
61-
const storedUser = JSON.parse(localStorage.getItem("user"));
6276

63-
if (
64-
storedUser &&
65-
storedUser.email === email &&
66-
storedUser.password === password
67-
) {
77+
const users = JSON.parse(localStorage.getItem("users")) || [];
78+
79+
const user = users.find(
80+
u => u.email === email && u.password === password
81+
);
82+
83+
if (user) {
6884
localStorage.setItem("loggedIn", "true");
69-
localStorage.setItem("loggedInUser", storedUser.username);
85+
localStorage.setItem("loggedInUser", user.username);
7086
window.location.href = getMainPagePath("index.html");
7187
} else {
7288
error.textContent = "Invalid email or password";
@@ -86,8 +102,8 @@ function showLoggedInUser() {
86102
const username = localStorage.getItem("loggedInUser");
87103
const userElement = document.getElementById("navUsername");
88104

89-
if (userElement && username) {
90-
userElement.textContent = username;
105+
if (userElement) {
106+
userElement.textContent = username ? username : "User";
91107
}
92108
}
93109

@@ -96,4 +112,5 @@ function logout() {
96112
localStorage.removeItem("loggedIn");
97113
localStorage.removeItem("loggedInUser");
98114
window.location.href = getMainPagePath("signin.html");
99-
}
115+
}
116+

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html lang="en">
33

44
<head>
@@ -167,13 +167,13 @@ <h3>Tic Tac Toe</h3>
167167

168168
<!-- AUTH SYSTEM -->
169169
<script src="auth.js"></script>
170+
<script src="script.js"></script>
170171
<script>
171172
checkAuth(); // protect page
172173
showLoggedInUser(); // show username
173174
</script>
174175

175-
<script src="script.js"></script>
176176

177177
</body>
178178

179-
</html>
179+
</html>

signin.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h2>Login</h2>
3333
<script src="script.js"></script>
3434
<script>
3535
if (localStorage.getItem("loggedIn")) {
36-
window.location.href = "index.html";
36+
window.location.href = getMainPagePath("index.html");
3737
}
3838
</script>
3939
</body>

signup.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h2>Create Account</h2>
4242
<script src="script.js"></script>
4343
<script>
4444
if (localStorage.getItem("loggedIn")) {
45-
window.location.href = "index.html";
45+
window.location.href = getMainPagePath("index.html");
4646
}
4747
</script>
4848
</body>

0 commit comments

Comments
 (0)