问题
I've run into a problem during development of email verification with firebase.
Whenever I try to verify the email address, I can't get the info that user's email address is verified.
I have a code like this:
constructor(private afAuth: AngularFireAuth, public navCtrl: NavController, public navParams: NavParams) {
}
ionViewWillEnter() {
this.afAuth.auth.onAuthStateChanged(user => {
if (user && user.emailVerified) {
this.navCtrl.setRoot(ShoppingListPage);
}
});
}
proceedButtonHandler() {
this.afAuth.auth.onAuthStateChanged(user => {
if (user && user.emailVerified) {
this.navCtrl.setRoot(ShoppingListPage);
}
});
}
But after I verify the email address and run proceedButtonHandler function, in user.emailVerified field I always get "false" result, unless I refresh the page.
What is the right way to know if user's email is currently verified? And is there any way to watch for "emailVerified" field changes and redirect the user to another page without clicking the button? (there is an attempt to do this in "ionViewWillEnter" function)
回答1:
If the user email is verified out of band via the email verification link, you need to do 2 things on the client after verification:
- call
currentUser.reload
. This will update the emailVerified property on the user. - call
currentUser.getIdToken(true)
to force token refresh or just wait for the token to be refreshed and automatically pick up the change. This is needed for theemail_verified
property on the token to update.
Firebase now allows you to return back to the app after email verification. Here is an example via the web client: https://firebase.google.com/docs/auth/web/passing-state-in-email-actions
You can use this when you return back to the app from such operation to reload the user/token knowing the user just verified their email. The above also allows you to pass state in the flow.
来源:https://stackoverflow.com/questions/46268776/ionic-3-firebase-doesnt-show-that-email-is-verified