@@ -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+ }
0 commit comments