问题
I've successfully implemented Google and Facebook sign ins for Flutter using firebase_auth 0.11.0. I need to implement the linking account feature for accounts that have the same email address.
Future<FirebaseUser> _signInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
How/where do you catch the Firebase error (auth/account-exists-with-different-credential) that the account is a duplicate email and therefore requires the linkwithcredential
function? I've tried to catch the error on signInWithCredential
and _signInWithGoogle()
above to no avail.
Based on the example here:
https://github.com/flutter/plugins/blob/06256967e494e6d719023a249c8bdaae4b3ae065/packages/firebase_auth/test/firebase_auth_test.dart
FirebaseUser user = await auth.currentUser();
user = await user.linkWithCredential(credential);
This is how you link the accounts, but my question is how do you determine that this function needs to be run?
Edit: for clarity, can I link the accounts when only one credential/session exists? For example, the flow should be, sign in with Facebook, catch (ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL), and then link to Google.
回答1:
Get the list of provider IDs for the current user, if it has FB but no Google, then Google can be linked to it
List<UserInfo> providerList = _auth.currentUser().providerData;
//Capture the error for the signin with crecentials, check the type of error, act /// Possible Errors:
ERROR_INVALID_CREDENTIAL
- If the credential data is malformed or has expired.ERROR_USER_DISABLED
- If the user has been disabled (for example, in the Firebase console)ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL
- If there already exists an account with the email address asserted by Google. Resolve this case by calling [fetchSignInMethodsForEmail] and then asking the user to sign in using one of them. This error will only be thrown if the "One account per email address" setting is enabled in the Firebase console (recommended).ERROR_OPERATION_NOT_ALLOWED
- Indicates that Google accounts are not enabled.ERROR_INVALID_ACTION_CODE
- If the action code in the link is malformed, expired, or has already been used.
final onError = (exception, stacktrace) {
/*ERROR HANDLING*/
};
final FirebaseUser user = await _auth.signInWithCredential(credential).catchError(onError);
回答2:
Based on this information: Linking multiple auth providers with Firebase on Login
(If it is still accurate) my flow is unattainable, i.e. you can't link accounts without having logged into the first firebase account, e.g. Login to Google THEN login and link Facebook.
The user flow I need will need to be achieved by enabling multiple accounts for the same email. Though this poses additional problems, the second firebase account user FirebaseUser user
does not contain an email, user.email
is null
. Though the user.providerData
has the email.
来源:https://stackoverflow.com/questions/56260249/how-do-you-link-firebase-accounts-in-firebase-auth-0-11-1-in-flutter