title: User can only be linked to one identity for the given provider
body: <p>I am building an application wherein user can <strong>log in anonymously</strong> and later can choose to <strong>login with Google or facebook</strong>. I've followed the steps mentioned in <strong><a href="https://firebase.google.com/docs/auth/web/anonymous-auth" rel="nofollow noreferrer">firebase site</a></strong> and also <strong><a href="https://stackoverflow.com/questions/46135248/firebase-auth-linking-error-user-can-only-be-linked-to-one-identity-for-the-gi">this post</a></strong> but still am getting an error which reads - </p>
  
  <blockquote>
    <p>code: "auth/provider-already-linked",<br>
    message: "User can only be
    linked to one identity for the given provider."</p>
  </blockquote>
  
  <p>Below are my <strong>auth.service.ts</strong> contents</p>
  
  <pre><code>@Injectable()
  export class AuthService {
    user: Observable&lt;User&gt;;
    constructor(private afAuth: AngularFireAuth,
      private afs: AngularFirestore,
      private router: Router) {
      this.user = this.afAuth.authState
        .switchMap(user =&gt; {
          if (user) {
            return this.afs.doc&lt;User&gt;(`usersData/${user.uid}`).valueChanges()
          } else {
            return Observable.of(null)
          }
      });
    }
  
    anonymousLogin(): Promise&lt;any&gt;{
      return this.afAuth.auth.signInAnonymously();
    }
  
    get authState(): Observable&lt;User&gt;{
      return this.afAuth.authState.map(data =&gt; {
        return data;
      });
    }
  
    get currentUser(): boolean {
      return this.user !== null;
    }
  
    getCurrentUser(): User{
      return this.afAuth.auth.currentUser;
    }
  
    googleLogin() {
      const provider = new firebase.auth.GoogleAuthProvider()
      return this.oAuthLogin(provider);
    }
  
    facebookLogin(){
      const provider = new firebase.auth.FacebookAuthProvider();
      provider.setCustomParameters({
        'display': 'popup'
      });
      return this.oAuthLogin(provider);
    }
  
    private oAuthLogin(provider) {
      return this.afAuth.auth.signInWithPopup(provider)
        .then((response) =&gt; {
          if(this.getCurrentUser() !== null){
            let cred = null;
            if(response.credential.providerId === 'google.com'){
              cred = firebase.auth.GoogleAuthProvider
                     .credential(response.credential.idToken);
            }else if(response.credential.providerId === 'facebook.com'){
              cred = firebase.auth.FacebookAuthProvider
                     .credential(response.credential.authToken);
            }
            firebase.auth().currentUser
            .linkWithCredential(cred).then(function(user) {
              console.log("Anonymous account successfully upgraded", user);
            }, function(error) {
              console.log("Error upgrading anonymous account", error);
            });
          }
          this.updateUserData(response.user)
        });
    }
  
    private updateUserData(user) {
      const userRef: AngularFirestoreDocument&lt;any&gt; = this.afs.doc(`usersData/${user.uid}`);
      const data: User = {
        uid: user.uid,
        email: user.email,
        displayName: user.displayName,
        photoURL: user.photoURL,
        isAnonymous: user.isAnonymous,
        roles: {
          customer: true
        }
      };
      return userRef.set(data, {merge: true});
    }
    signOut() {
      this.afAuth.auth.signOut().then(() =&gt; {
  
      });
    }
  }
  </code></pre>
  
  <p>As suggested in other posts referred I've used <code>linkWithCredential</code> method by passing required <code>idToken/accessToken</code> but it is failing to link the accounts on both providers and hence end up creating multiple accounts under <strong>Authentication-> Users</strong>.</p>
  
  <p>Could anyone please point me in the right direction or am I missing anything here?</p>