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();
}
});