Skip to content

[firebase_auth] linkWithCredential uses current user instead of user object on which the function is called #48

Closed
@jeanmatthieud

Description

@jeanmatthieud

I'm trying to manage the 'ERROR_CREDENTIAL_ALREADY_IN_USE' error when linking an account to another, by following the android guide: https://firebase.google.com/docs/auth/web/account-linking#link-email-address-and-password-credentials-to-a-user-account

In the last code section, when the credentials are already in use:

  • User logged in with account [A], and submitted credentials [C]
  • SignIn on the already existing account [B] with credentials [C]
  • Retrieve app's data linked to account [B]
  • Delete the existing account [B]
  • Link user account [A] to the credentials [C] and SignIn with credentials [C]
  • Import data of account [B] on the linked user

Ugly code:

AuthResult authResult;
final FirebaseUser previousUser = await FirebaseAuth.instance.currentUser();
try {
  authResult = await previousUser.linkWithCredential(credential);
} on PlatformException catch (err) {
  if (err.code == 'ERROR_CREDENTIAL_ALREADY_IN_USE') {
    // Signin to the existing account
    authResult = await FirebaseAuth.instance.signInWithCredential(credential);
    // TODO : Get data from the authResult.user account
    // Delete existing account
    // TODO : better deletion error management
    await authResult.user.delete();
    // Link previous user
    authResult = await previousUser.linkWithCredential(credential);
    // TODO : merge previous account data to current account
  } else {
    throw err;
  }
}

Sadly, calling previousUser.linkWithCredential() seems to use firebaseAuth.getCurrentUser() and not the user on which the call has been done, resulting into an USER_REQUIRED error.
See FirebaseAuthPlugin.java:275

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocked: customer-responseWaiting for customer response, e.g. more information was requested.impact: customerA bug with low impact (e.g. affecting only a few customers or has a workaround). (P3)plugin: authresolution: needs-reproThis issue could not be reproduced or needs an up to date reproduction on latest FlutterFire plugin.type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions