Skip to content

Commit cf6a979

Browse files
committed
true commit
1 parent 90564e6 commit cf6a979

4 files changed

Lines changed: 22 additions & 38 deletions

File tree

auth.js

Lines changed: 16 additions & 32 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.trim();
32-
const email = document.getElementById("email").value.trim();
31+
const username = document.getElementById("username").value;
32+
const email = document.getElementById("email").value;
3333
const password = document.getElementById("password").value;
3434
const confirmPassword = document.getElementById("confirmPassword").value;
3535
const error = document.getElementById("signupError");
@@ -41,25 +41,10 @@ if (signupForm) {
4141
return;
4242
}
4343

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

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");
47+
window.location.href = getMainPagePath("signin.html");
6348
});
6449
}
6550

@@ -70,19 +55,18 @@ if (signinForm) {
7055
signinForm.addEventListener("submit", (event) => {
7156
event.preventDefault();
7257

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

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) {
63+
if (
64+
storedUser &&
65+
storedUser.email === email &&
66+
storedUser.password === password
67+
) {
8468
localStorage.setItem("loggedIn", "true");
85-
localStorage.setItem("loggedInUser", user.username);
69+
localStorage.setItem("loggedInUser", storedUser.username);
8670
window.location.href = getMainPagePath("index.html");
8771
} else {
8872
error.textContent = "Invalid email or password";
@@ -102,8 +86,8 @@ function showLoggedInUser() {
10286
const username = localStorage.getItem("loggedInUser");
10387
const userElement = document.getElementById("navUsername");
10488

105-
if (userElement) {
106-
userElement.textContent = username ? username : "User";
89+
if (userElement && username) {
90+
userElement.textContent = username;
10791
}
10892
}
10993

@@ -112,4 +96,4 @@ function logout() {
11296
localStorage.removeItem("loggedIn");
11397
localStorage.removeItem("loggedInUser");
11498
window.location.href = getMainPagePath("signin.html");
115-
}
99+
}

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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>
171170
<script>
172171
checkAuth(); // protect page
173172
showLoggedInUser(); // show username
174173
</script>
175174

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

177177
</body>
178178

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

signin.html

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

signup.html

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

0 commit comments

Comments
 (0)