cb = function(user) {
    if (user &&
        // If email already exists, user must first be signed in to the
        // existing account on the internal Auth instance before the credential
        // is linked and merge conflict is triggered.
        !firebaseui.auth.storage.getPendingEmailCredential(self.getAppId())) {
      // For anonymous user upgrade, call link with popup on the external Auth
      // user. Otherwise merge conflict will always occur when linking the
      // credential to the external anonymous user after creation.
      return /** @type {!firebase.Promise<!firebase.auth.UserCredential>} */ (
          user.linkWithPopup(provider)
          .then(function(result) {
            return result;
          }, function(error) {
            // Fail directly when email already in use error thrown. This will
            // trigger account linking flow.
            // When email already exists for a new federated credential, user
            // must sign to existing account and then link this credential to
            // that account.
            if (error &&
                error['code'] == 'auth/email-already-in-use' &&
                error['email'] && error['credential']) {
              throw error;
            }
            // For all other errors, run onUpgrade check.
            return self.onUpgradeError(error);
          }));
    } else {
      // Starts sign in with popup. This runs on the internal Auth instance as
      // finish sign in will sign in with the final credential to developer Auth
      // instance.
      return self.getAuth().signInWithPopup(provider);
    }
  }
Signs in to Auth provider via popup.
@param {!firebase.auth.AuthProvider} provider The Auth provider to sign in
    with.
@return {!firebase.Promise<!firebase.auth.UserCredential>}