简体   繁体   中英

Flutter Firebase authentication: reverting back to a specific anonymous user

The Firebase authentication documentation for Kotlin states that:

The call to linkWithCredential will fail if the credentials are already linked to another user account. In this situation, you must handle merging the accounts and associated data as appropriate for your app:

And then a code example is provided, the Flutter equivalent of which could be written as follows:

final prevUser = FirebaseAuth.instance.currentUser;
try {
  final result = await FirebaseAuth.instance.signInWithCredential(credential);
  final currentUser = result.user;
  // Merge prevUser and currentUser accounts and data
  // ...  
} catch (e) {
    // ...
}

Let's assume prevUser is an anonymous user. If the merge of prevUser and currentUser accounts and data doesn't succeed, is there any way to restore the Firebase sign-in state to what it was prior to the call to signInWithCredential , ie to make prevUser the currently signed in Firebase user again?

My app backend has its own representation of a user - AppUser - that is keyed by Firebase user uid, and there is an AppUser user that corresponds to (ie has the same uid as) prevUser . I don't want to recover from the failed merge by signing in anonymously, because that would give me a new anonymous user, with a different uid, which doesn't match up with said AppUser user.

No. Once an anonymous user signs out, or becomes replaced with a new sign-in, that anonymous account is effectively lost forever, from the perspective of the client app. The client app was the only holder of credentials that could perform a sign-in on that account.

You might want to consider at least persisting the uid of that account, then provide it to whatever process performs the merge. This would typically be done on a backend so that it could bypass any security restrictions on what can be done with an account when the user is not controlling it directly from a client app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM