You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
try{// Create user with email and passwordconstuserCredential=awaitcreateUserWithEmailAndPassword(auth,email,password);// Signed upconstuser=userCredential.user;// Update user profile with name and surnameawaitupdateProfile(auth.currentUser,{displayName: `${name}${surname}`,});// Additional actions after sign-up if neededwindow.location.replace("http://"+host+"/dashboard");}catch(error){// Handle sign-up errorsconsterrorCode=error.code;consterrorMessage=error.message;console.error("Sign-up error:",errorMessage);}
3.2 Sign in with Email / Password
try{constuserCredential=awaitsignInWithEmailAndPassword(auth,email,password);// Signed inconstuser=userCredential.user;// Additional logic here if neededconsole.log("User signed in successfully.");window.location.replace("http://"+host+"/dashboard");}catch(error){// Handle sign-in errorsconsterrorCode=error.code;consterrorMessage=error.message;console.error("Sign-in error:",errorMessage);throwerror;}
3.3 Sign Out
try{awaitsignOut(auth);console.log('sign out success!');window.location.replace("http://"+host+"/login");}catch(error){// An error happened.console.error("Sign-out error:",error);}
3.4 Auth State Listener
onAuthStateChanged(auth,function(user){if(user){// User is signed inconsole.log('User is signed in');if(window.location.pathname==='/dashboard'){// If the current page is the dashboard, stay on the dashboardreturn;}else{// Redirect to the dashboardwindow.location.replace("http://"+host+"/dashboard");}}else{// No user is signed inconsole.log('No user is signed in');if(window.location.pathname==='/login'||window.location.pathname==='/signup'){// If the current page is the login or signup page, stay therereturn;}else{// Redirect to the login pagewindow.location.replace("http://"+host+"/login");}}});
4.0 Firestore
4.1 Add / Set Document
// Add a document to a collection (with an auto assigned doc ID)constuid=awaitgetUserUID();awaitaddDoc(collection(db,"some-collection"),{mapName: "Los Angeles"});
import{query,where,collection}from"firebase/firestore";// Initialize auth && firestore with the 'firebaseApp' propertyconstdb=getFirestore(app);// Collection/doc refconstcollectionRef=collection(db,"some-collection");// Use a query with collection listenerconstcollectionListener=(collection)=>{constcollectionQuery=query(collectionRef,where("some-data","==",true));onSnapshot(collectionQuery,(querySnapshot)=>{letdataArray=[];querySnapshot.forEach((doc)=>{dataArray.push(doc.data());});dataArray.map((d)=>console.log(d));});};