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