Flutter How to Link Multiple Auth Providers to an Firebase Account?

Viewed 1296

I am using firebase in my flutter application. In my app user can sign in or register using 3 ways.

  1. Email id and password.
  2. Google
  3. Facebook.

This is my Firebase setting. enter image description here

This is my user collection. enter image description here

Now my doubt is that how do I link multiple auth providers for a user having same email id ?

I am storing user's info into User collection with Uid. If I enable multiple account to multiple providers how can I store user's data in same document ?

I have search a lot on google but didn't find proper solution.

For registration with email ID I am using this.

  final newUser = await _auth.createUserWithEmailAndPassword(
          email: widget.email, password: widget.pass);

Then I am storing user's data into User collection. (In this I have one more form, from where I am getting other data).

Google sign in code

  Future<User> signInWithGoogle() async {
    // Trigger the authentication flow
    final GoogleSignInAccount googleUser = await _googleSignIn.signIn();

    // Obtain the auth details from the request
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    // Create a new credential
    final GoogleAuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    // Once signed in, return the UserCredential
    final UserCredential authResult = await _firebaseAuth.signInWithCredential(credential);
    final User user = authResult.user;

    print('authResult');
    print(authResult);

    return user;
  }
0 Answers
Related