Skip to content

Commit

Permalink
email verification with firebase auth
Browse files Browse the repository at this point in the history
  • Loading branch information
charakamihiranga committed Sep 4, 2024
1 parent 4bc2a6c commit 615eb0b
Showing 1 changed file with 58 additions and 46 deletions.
104 changes: 58 additions & 46 deletions Login_With_Firebase/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 615eb0b

Please sign in to comment.