androidfirebaseflutterfirebase-authentication

Flutter: How to get Firebase Auth Credentials to update email and password


So, In my flutter app, I am trying to add functionality to change email.

I used userData.updateEmail(email) method, but it gives this error:

Unhandled Exception: PlatformException(ERROR_REQUIRES_RECENT_LOGIN, This operation is sensitive and requires recent authentication. Log in again before retrying this request., null)

On surfing for a solution on the Internet I got to know, I need to reauthenticate user by this method: userData.reauthenticateWithCredential(credential)

But I can't find a way to get credential to pass to reauthenticateWithCredential method.

Some code snippets (though I feel they are unnecessary):

initUserData() async {
  FirebaseUser user = await FirebaseAuth.instance.currentUser();

  setState(() {
    userData = user;
  });
}

updateEmail(String value) async {
  // value is the email user inputs in a textfield and is validated
  userData.updateEmail(value);
}

Note: I am using both login with google and password-email login.


Solution

  • I had this problem too and I found the solution. You can get the AuthCredential for the providers you're using like this:

    EmailAuthProvider.getCredential(email: 'email', password: 'password');
    

    and

    GoogleAuthProvider.getCredential(idToken: '', accessToken: '')
    

    Both methods return what you're looking for.