diff --git a/Login_With_Firebase/js/app.js b/Login_With_Firebase/js/app.js index ae5c2a3..81e6627 100644 --- a/Login_With_Firebase/js/app.js +++ b/Login_With_Firebase/js/app.js @@ -13,46 +13,58 @@ const firebaseConfig = { firebase.initializeApp(firebaseConfig); // Sign up function -function userRegistration(event) { +function userRegistration() { + const email = document.getElementById('email').value; const password = document.getElementById('password').value; firebase.auth().createUserWithEmailAndPassword(email, password) - .then((userCredential) => { - // Signed up - const user = userCredential.user; - console.log("User signed up:", user); - alert("Sign-up successful!"); - - // Clear the form fields - document.getElementById('signupForm').reset(); + .then((userCredential) => { + const user = userCredential.user; + // Send email verification + user.sendEmailVerification().then(() => { + console.log("Verification email sent."); + alert("Sign-up successful! Please check your email to verify your account."); + + // Clear the form fields + document.getElementById('signupForm').reset(); - // Optionally, redirect to a welcome page or sign-in page - window.location.href = "signin.html"; - }) - .catch((error) => { - handleAuthError(error); + // Optionally, redirect to a sign-in page + window.location.href = "signin.html"; }); + }) + .catch((error) => { + handleAuthError(error); + }); } // Sign in function -function userSignIn(event) { +function userSignIn() { + const email = document.getElementById('email').value; const password = document.getElementById('password').value; firebase.auth().signInWithEmailAndPassword(email, password) - .then((userCredential) => { - // Signed in - const user = userCredential.user; - console.log("User signed in:", user); - alert("Sign-in successful!"); + .then((userCredential) => { + const user = userCredential.user; + if (user.emailVerified) { + console.log("User signed in:", user); + alert("Sign-in successful!"); - // Clear the form fields - document.getElementById('loginForm').reset(); - }) - .catch((error) => { - handleAuthError(error); - }); + // Clear the form fields + document.getElementById('loginForm').reset(); + + } else { + // Sign out the user if email is not verified + firebase.auth().signOut().then(() => { + alert("Please verify your email before signing in."); + + }); + } + }) + .catch((error) => { + handleAuthError(error); + }); } // Function to handle authentication errors @@ -63,25 +75,25 @@ function handleAuthError(error) { // Custom error handling switch (errorCode) { - case 'auth/invalid-email': - alert('Invalid email format. Please check your email.'); - break; - case 'auth/user-disabled': - alert('This user has been disabled.'); - break; - case 'auth/user-not-found': - alert('No user found with this email.'); - break; - case 'auth/wrong-password': - alert('Incorrect password. Please try again.'); - break; - case 'auth/email-already-in-use': - alert('This email is already in use by another account.'); - break; - case 'auth/weak-password': - alert('The password is too weak. Please choose a stronger password.'); - break; - default: - alert('An unexpected error occurred: ' + errorMessage); + case 'auth/invalid-email': + alert('Invalid email format. Please check your email.'); + break; + case 'auth/user-disabled': + alert('This user has been disabled.'); + break; + case 'auth/user-not-found': + alert('No user found with this email.'); + break; + case 'auth/wrong-password': + alert('Incorrect password. Please try again.'); + break; + case 'auth/email-already-in-use': + alert('This email is already in use by another account.'); + break; + case 'auth/weak-password': + alert('The password is too weak. Please choose a stronger password.'); + break; + default: + alert('An unexpected error occurred: ' + errorMessage); } }