Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link Multiple Auth Providers to an Account

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!

like image 495
FlutterFirebase Avatar asked Apr 16 '19 20:04

FlutterFirebase


Video Answer


1 Answers

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.

like image 126
Frank van Puffelen Avatar answered Oct 20 '22 14:10

Frank van Puffelen