Closed
Description
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