Reputation: 31
I am having issues with my email verification. My error is "user is null" so it doesnt send the verification email, but it does show a log in uid in the console and my console.firebase.google.com project shows the signed up email. What should I change so an email is sent and no access is given until user has verified their email? I've read the docs but cant figure it out. Thank you in advance.
//add create user event
btnSignUp.addEventListener('click', e => {
//get email and Password
const email = txtEmail.value;
const pass = txtPassword.value;
const auth = firebase.auth();
const promise = auth.createUserWithEmailAndPassword(email, pass);
var user = firebase.auth().currentUser;
user.sendEmailVerification();
promise.catch(e => console.log(e.message));
});
//Add a realtime listener
firebase.auth().onAuthStateChanged(function(user) {
if (user.emailVerified) {
console.log('Email is verified');
console.log(user);
} else {
window.location = "index.html";
firebase.auth().signOut();
alert("Email is not verified");
}
Upvotes: 3
Views: 6472
Reputation: 798
For the Firebase version 9
, I am currently using -
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
sendEmailVerification(user)
.then(() => {
// Email verification sent!
let msg = 'An email verification link has been sent to ' + user.email;
document.querySelector('.success.email_msg').innerHTML=msg;
});
})
.catch((error) => {
document.querySelector('.error.reg_error').innerHTML = error.message;
});
Upvotes: 3
Reputation: 30868
You are not waiting for the promise to resolve. Update to the following snippet:
const promise = auth.createUserWithEmailAndPassword(email, pass);
promise.then(user => {
user.sendEmailVerification();
}).catch(error => console.log);
Upvotes: 1