简体   繁体   中英

Firebase verifying email while being logged in

I have the following logic in my iOS app:

  • User registers
  • Firebase sends an email confirmation
  • Returns to login screen

Now if the user logs in, without verifying the email, then we have a user session and isEmailVerified is false. I only need to check the isEmailVerified in a certain point in the app. Also I think signing the user in, checking the field and signing the user out would be bad practise.

I'd need to reauthenticate the user, what is the best way of doing this? How can I, after the user has logged in, switch the status of isEmailVerified ?

Thanks

First, you need to have the email and password to create a credential. Your user already provided this on the login page... So the email and password to persistent storage on iOS. In Android, the equivalent would be SharedPreferences.

I do not code in iOS, but this will give you the idea for the logic.

Then, when you get to that point in your app where email verified is called:

if (user.isEmailVerified) == true {
   // you do not need to hold the email and password in persistent storage anymore.
   // go into your persistent storage and delete the data.
} else {
   // get the email and password the user saved in persistent storage.
   String email = persistentStorage.getEmail();
   String password = persistentStorage.getPassword();

    var user = firebase.auth().currentUser;
    var credentials = firebase.auth.EmailAuthProvider.credential(email, password);
    user.reauthenticate(credentials);

    // then, when finished reauthenticating, check whether isEmailVerified() == true;


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM