@@ -3,7 +3,6 @@ import { initializeApp } from "https://www.gstatic.com/firebasejs/9.16.0/firebas
33import { getAuth , createUserWithEmailAndPassword , signInWithEmailAndPassword , onAuthStateChanged , signOut } from "https://www.gstatic.com/firebasejs/9.16.0/firebase-auth.js" ;
44
55// Your Firebase configuration
6- // For Firebase JS SDK v7.20.0 and later, measurementId is optional
76const firebaseConfig = {
87 apiKey : "AIzaSyCf3OuJ3LfQIOrY_ytB-VMwPTm2jaKIBJ4" ,
98 authDomain : "ubercoders.firebaseapp.com" ,
@@ -26,17 +25,14 @@ signupForm.addEventListener("submit", async (e) => {
2625 const username = document . getElementById ( "signup-username" ) . value . trim ( ) ;
2726 const email = document . getElementById ( "signup-email" ) . value . trim ( ) ;
2827 const password = document . getElementById ( "signup-password" ) . value ;
28+
2929 function validatePassword ( password ) {
30- // At least 8 characters long
31- // Contains uppercase and lowercase letters
32- // Contains at least one number
33- // Contains at least one special character
34- const passwordRegex = / ^ (? = .* [ a - z ] ) (? = .* [ A - Z ] ) (? = .* \d ) (? = .* [ @ $ ! % * ? & ] ) [ A - Z a - z \d @ $ ! % * ? & ] { 8 , } $ / ;
35- return passwordRegex . test ( password ) ;
36- }
30+ // At least 6 characters long
31+ return password . length >= 6 ;
32+ }
3733
3834 if ( ! validatePassword ( password ) ) {
39- alert ( "Password must be at least 8 characters long, contain uppercase and lowercase letters, at least one number, and at least one special character ." ) ;
35+ alert ( "Password must be at least 6 characters long." ) ;
4036 return ;
4137 }
4238
@@ -81,23 +77,25 @@ loginForm.addEventListener("submit", async (e) => {
8177} ) ;
8278
8379// Detect User Authentication State
84- if ( window . location . pathname . endsWith ( "index.html" ) || window . location . pathname === "/" ) {
85- if ( user ) {
86- // User is logged in
87- console . log ( "User is logged in:" , user . email ) ;
88- // If already on login or signup page, redirect to dashboard
89- if ( window . location . href . includes ( "index.html" ) || window . location . href . endsWith ( "/" ) ) {
90- window . location . href = "dashboard.html" ;
91- }
92- } else {
93- // User is logged out
94- console . log ( "User is logged out" ) ;
95- // If on dashboard page, redirect to login page
96- if ( window . location . href . includes ( "dashboard.html" ) ) {
97- window . location . href = "index.html" ;
80+ onAuthStateChanged ( auth , ( user ) => {
81+ if ( window . location . pathname . endsWith ( "index.html" ) || window . location . pathname === "/" ) {
82+ if ( user ) {
83+ // User is logged in
84+ console . log ( "User is logged in:" , user . email ) ;
85+ // If already on login or signup page, redirect to dashboard
86+ if ( window . location . href . includes ( "index.html" ) || window . location . href . endsWith ( "/" ) ) {
87+ window . location . href = "dashboard.html" ;
88+ }
89+ } else {
90+ // User is logged out
91+ console . log ( "User is logged out" ) ;
92+ // If on dashboard page, redirect to login page
93+ if ( window . location . href . includes ( "dashboard.html" ) ) {
94+ window . location . href = "index.html" ;
95+ }
9896 }
9997 }
100- } ;
98+ } ) ;
10199
102100// Handle Log Out
103101const logoutButton = document . getElementById ( "logout-button" ) ;
0 commit comments