Reputation:
$("#btn1").on('click', function(e) {
e.preventDefault(); // <== disable form submit
const email = signupForm['signup-email'].value;
const password = signupForm['signup-password'].value;
// sign up the user & add firestore data
const auth = firebase.auth();
const promise = auth.createUserWithEmailAndPassword(email, password);
promise.then(user => {
user = firebase.auth().currentUser;
user.sendEmailVerification();
}).catch(error => console.log);
});
$("#btn2").on('click', function(e) {
e.preventDefault(); // <== disable form submit
var user = firebase.auth().currentUser;
if (user.emailVerified) {
// email is verified.
console.log("email is verified")
} else {
// email is not verified.
console.log("email is not verified")
}
});
I want that my Website sends an email to the user after the user enters his email and password.
In my Code, user.sendEmailVerification();
works fine and the user gets an email.
If the user verifies his email and clicks the btn2, the console should print "email is verified", but this doesn't happen. The console always prints "email is not verified". I tried also the firebase.auth().onAuthStateChanged
method, but it's the same.
var user = firebase.auth().currentUser;
firebase.auth().onAuthStateChanged(user => {
if(user.emailVerified){
console.log('email is verified')
}else{
console.log('email not verified')
}
})
Upvotes: 3
Views: 2535
Reputation: 345
On the process of authentication, you are redirected to the auth server, and gets back to the page. The onAuthStageChange
it's a observer to that redirection, you don't need to trigger an event, since when it's a realtime event.
Upvotes: 0
Reputation: 598827
Verifying the email address happens out-of-band, typically in another tab of the same browser or in another application altogether. This means that your application code isn't immediately made aware of the update to the user's profile, but only once one of these things happens:
reload
on their user profile.Once any of these happens, the user profile will contain the latest information from the server, including the user's email verification state.
Also see:
Upvotes: 3