Reputation: 129
Well, I've tried using many ways to trigger the function sendEmailVerification()
. But none have worked successfully. The docs aren't helping either.
Below is part of the source code that I will be using. Please let me know of how I can correct this.
On my console I get the the following error:
TypeError: Cannot read property 'emailVerified' of null at Object.firebase.auth.onAuthStateChanged.firebaseUser [as next]
btnSignUpWithGoogle.addEventListener('click', e => { var provider = new firebase.auth.GoogleAuthProvider(); firebase.auth().signInWithPopup(provider).then(function(result) { var token = result.credential.accessToken; var user = result.user; }).catch(function(error) { var errorCode = error.code; var errorMessage = error.message; var email = error.email; var credential = error.credential; console.log(errorCode); }); }); btnLogin.addEventListener('click', e => { const email = txtEmail.value; const pass = txtPassword.value; const auth = firebase.auth(); const promise = auth.signInWithEmailAndPassword(email, pass); promise.catch(e => console.log(e.message)); txtEmail.value = ""; txtPassword.value = ""; }); btnSignUp.addEventListener('click', e => { const email = txtEmail.value; const pass = txtPassword.value; const auth = firebase.auth(); const promise = auth.createUserWithEmailAndPassword(email, pass); promise.catch(e => console.log(e.message)); txtEmail.value = ""; txtPassword.value = ""; const emailVerified = firebaseUser.emailVerified; if (!emailVerified){ firebase.auth().firebaseUser.sendEmailVerification().then(function(){ alert('Please check your email to verify your Account.'); }); } else { alert('Your Email has been verified!'); } }); firebase.auth().onAuthStateChanged(firebaseUser => { if (firebaseUser) { console.log(firebaseUser); console.log('Logged IN!'); btnLogout.style.visibility = 'visible'; } if (firebaseUser.emailVerified) { console.log('Email is verified'); } else { console.log('Email is not verified'); firebaseUser.sendEmailVerification(); } });
Upvotes: 4
Views: 1288
Reputation: 315
sendEmailVerification() only works once you have the email address already loaded into firebase.
Check the below code : https://github.com/aqeelsmith/AngularJsData/blob/master/builds/angulardata/js/services/authentication.js - Line 82
Upvotes: 0
Reputation: 3598
Can we see all the code? Are you sure that your firebase.auth() is declared properly?
Your auth should look like this:
const firebaseApp = firebase.initializeApp(firebaseConfig, 'Client');
const firebaseAuth = firebaseApp.auth();
firebaseUser should have emailVerified key. Move your if else statement inside if firebaseUser like this:
firebase.auth().onAuthStateChanged(firebaseUser => {
if (firebaseUser) {
console.log(firebaseUser);
console.log('Logged IN!');
btnLogout.style.visibility = 'visible';
if (firebaseUser.emailVerified) {
console.log('Email is verified');
}
else {
console.log('Email is not verified');
firebaseUser.sendEmailVerification();
}
}
});
console.log(firebaseUser)
, what is says?
Upvotes: 3