skip to Main Content

I am trying to convert a firebase user from Anonymous to permanent using email/password and running into errors.

The documentation and other articles state that I should linkWithProvider but it throws an exception that the email should be verified first, however, doing so throws another exception asking me to login first.

Here are snippets of what I tried so far

final emailAuthCredential = EmailAuthProvider.credential(
  email: email,
  password: password,
);

await currentUser.linkWithProvider(emailAuthCredential);

FirebaseAuthException ([firebase_auth/operation-not-allowed] Please verify the new email before changing email.)

final emailAuthCredential = EmailAuthProvider.credential(
  email: email,
  password: password,
);

await currentUser.verifyBeforeUpdateEmail(email);

FirebaseAuthException ([firebase_auth/requires-recent-login] This operation is sensitive and requires recent authentication. Log in again before retrying this request.)

2

Answers


  1. Chosen as BEST ANSWER

    The solution was in the documentation (duh...). You now have to use the identity platform API and call signup with the anonymous token.


  2. You can use this code

    linkUser()async{
    User? user = FirebaseAuth.instance.currentUser;
    
    if (user != null && user.isAnonymous) {
      try {
        // Prompt the user to enter their email and password
        String email = '[email protected]'; // Replace with the user's input
        String password = 'password123';   // Replace with the user's input
    
        // Link the user to the email and password provider
        AuthCredential credential = EmailAuthProvider.credential(
          email: email,
          password: password,
        );
        await user.linkWithCredential(credential);
    
        // Now, the user is converted to a permanent user with email and password
      } catch (e) {
        print('Error linking anonymous user: $e');
      }
    }
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search