6
votes

I have implemented Facebook and Google sign in.

But FireBase document says this will cause an error if the same user first signs up with Facebook and later try sign in with Google (with the same email).

So I follow doc and try to configure account linking.

But I do not know how to do.

Should I try link account every time user is logged in? Problem is I not know if the user already has signed in with another auth provider.

For example, the original code has:

Google:

  void _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);
  }

Facebook:

  void _signInWithFacebook() async {
    final AuthCredential credential = FacebookAuthProvider.getCredential(
      accessToken: _tokenController.text,
    );
    final FirebaseUser user = await _auth.signInWithCredential(credential);
  }

Is correct to call every time in _signInWithFacebook() and _signInWithGoogle() :

user = await auth.linkWithCredential(credential);

For example:

  void _signInWithFacebook() async {
    final AuthCredential credential = FacebookAuthProvider.getCredential(
      accessToken: _tokenController.text,
    );
    final FirebaseUser user = await _auth.signInWithCredential(credential);

user = await auth.linkWithCredential(credential); //new

  }

How I can implement correctly?

Thanks!

1
I have used this concept using java code. At first, I was unable to login with the same email using different providers . Later I could able to do with both providers using the same email. using this stackoverflow.com/a/54802267/7352857Brahma Datta
@g.brahmaDatta Thanks for reply! but I am not try retrieve email. I just want link 2 auth provider: Facebook and GoogleFlutterFirebase
Oh you want to Implement both facebook and google signin with same email?Brahma Datta
@g.brahmaDatta Thanks for reply! Yes I need link both when user sign inFlutterFirebase
Check my answer below and let me know if that's working for you. @FlutterFirebaseBrahma Datta

1 Answers

1
votes

When the user enters their email address to sign in, you'll want to use fetchProvidersForEmail() to find out if that email address is already known.

If a user has already signed up with another provider, that's a good moment to ask them if they want to merge those accounts, and then call the account linking API.