dimanche 6 juin 2021

How do I verify text in a Javascript if then function?

Im trying to write a script that says if you enter the word "dog" in the promo box, then an id will be created in firebase. I'm testing out my script and when I enter "dog" the script doesn't proceed to creating an id and I get the else pop up, "please enter a promo code."

/

/ Get elements
const txtEmail = document.getElementById('email');
const txtPassword = document.getElementById('password');
const btnLogin = document.getElementById('btnlogin');
const btnSignUp = document.getElementById('btnsignup');
const btnLogout = document.getElementById('btnsignout');
const txtPromo = document.getElementById('promo');

// Add login event
btnLogin.addEventListener('click', e => {
  console.log("logged in");
    // Get email and password
    const email = txtEmail.value;
    const password = txtPassword.value;
    const auth = firebase.auth();
    // Sign in
    const promise = auth.signinwithemailandpassword(email,password);
    promise.catch(e => console.log(e.message));
});

// Add signup event
btnSignUp.addEventListener('click', e => {
if (promo === "dog") {
  alert ("Your account has been created. Please login.");      
  console.log("account created");
    // Get email and password
    const email = txtEmail.value;
    const password = txtPassword.value;
    const auth = firebase.auth();
    console.log(email);
    console.log(password);
    //console.log(promo);//
    auth.createUserWithEmailAndPassword(email, password)
  .then((userCredential) => {
    // Signed in 
    var user = userCredential.user;
    console.log(user);
    window.location.href = "login.html";
    // ...
  })
  .catch((error) => {
    //alert ("The email address is already in use by another account.");//
    var errorCode = error.code;
    var errorMessage = error.message;
    console.log(errorMessage);
    // ..
  });}
    // Sign up
    //const promise = auth.createuserwithemailandpassword(email,password);
 
    //promise.catch(e => console.log(e.message));
  else {alert ("Please enter a promo code."); console.log("need promo")};
});

Aucun commentaire:

Enregistrer un commentaire