Search code examples
firebasereact-nativefirebase-authenticationreact-native-firebase

How to persist Firebase UserCredential object, so can use UserCredential.sendEmailVerification() function


Firebase UserCredential object is generated when user logs in.

I want to use the function UserCredential.sendEmailVerification() to generate email to user if they have not previously verified their email address.....however I do not want to do this as soon as user has logged in. Instead I want to wait until they want to unlock some further functionality in the app....and only then do I want to send the verification email by running the UserCredential.sendEmailVerification() function.

Problem is....how do I persist the UserCredential object so that I have access to the sendEmailVerification() function everywhere in the app?

Is it possible to store as state?


Solution

  • You can get the signed in user at any time by using the code from the first snippet in the documentation on getting the current user:

    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        if (!user.emailVerified) {
          user.sendEmailVerification();
        }
      }
    });