2017-06-14 90 views
5

我已經在我的應用程序中使用Firebase身份驗證實施了兩步身份驗證,其中我使用了Gmail,Facebook或簡單的電子郵件登錄進行身份驗證。由於數字電話驗證已遷移到Firebase,我已通過將現有登錄帳戶(臉書,Gmail或電子郵件)與電話身份驗證憑證關聯起來實現了Firebase電話身份驗證。使用Facebook和電子郵件帳戶時,它可以正常工作。當用戶通過谷歌登錄並試圖通過電話身份驗證來驗證移動這個日誌印刷:Firebase電話身份驗證憑證鏈接谷歌登錄在Firebase

signInWithCredential:失敗

com.google.firebase.auth.FirebaseAuthUserCollisionException:一個帳戶已使用相同的存在電子郵件地址,但不同的登錄憑據。使用與此電子郵件地址關聯的提供商登錄。

閱讀本文article。是否與article中提到的問題相同?是否有任何解決方案相同..

回答

-1

此異常是因爲您已連接電子郵件登錄和Facebook登錄在一起,具有相同的電子郵件中使用的Facebook帳戶的谷歌帳戶沒有關聯在一起。默認情況下,Firebase不允許多次使用同一封電子郵件發生此衝突。

爲了解決這個問題,你有兩個選擇

1.鏈接的谷歌帳戶的Facebook和電子郵件一個人的使用

mAuth.getCurrentUser().linkWithCredential(credential); 

添加新的憑證到現有的登錄用戶。

2.從火力控制檯

啓用從相同的電子郵件(不推薦)多帳戶這將使新的UID爲谷歌用戶登錄和之前的Facebook登錄的用戶將有舊的。

+0

請仔細閱讀問題。我已經使用所有支持的設置實現了'linkWithCredential()'功能,並且我不想創建多個帳戶。我的手機身份驗證有問題。 –

1

經過互聯網和firebase文檔本身的研究後,我發現解決方案使用Firebase身份驗證的應用程序中的這兩步驗證。

firebaseAuth.getCurrentUser().updatePhoneNumber(credential).addOnCompleteListener(this, new OnCompleteListener<Void>() { 
     @Override 
     public void onComplete(@NonNull Task<Void> task) { 
      if (task.isSuccessful()) { 
       Log.d(TAG, "signInWithCredential:success"); 

       Snackbar.make(findViewById(android.R.id.content), "Mobile Verified Successfully.", 
         Snackbar.LENGTH_SHORT).show(); 

      } else { 
       Log.w(TAG, "signInWithCredential:failure", task.getException()); 
       if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { 
        //mVerificationField.setError("Invalid code."); 
        Snackbar.make(findViewById(android.R.id.content), "Invalid Code.", 
          Snackbar.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(context,"signInWithCredential:failure"+task.getException(), 
          Snackbar.LENGTH_LONG).show(); 
       } 
      } 
     } 
    }); 

只需通過​​以上方法,它會驗證電話分配到您的現有帳戶。確保它沒有被任何其他帳戶使用。

PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); 
0

現在手機權威性是firebase.Here可以是使用火力地堡代碼電話驗證: 如果問我任何問題的費用。

EditText phoneNum,Code;   //// two edit text one for enter phone number other for enter OTP code 
Button sent_,Verify;     // sent_ button to request for verification and verify is for to verify code 
private PhoneAuthProvider.ForceResendingToken mResendToken; 
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks; 
private FirebaseAuth mAuth; 
private String mVerificationId; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_phone_number_auth); 

    phoneNum =(EditText) findViewById(R.id.fn_num); 
    Code =(EditText) findViewById(R.id.code); 

    sent_ =(Button)findViewById(R.id.sent_nu); 
    Verify =(Button)findViewById(R.id.verify); 

    callback_verificvation();         




    mAuth = FirebaseAuth.getInstance(); 



    sent_.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String num=phoneNum.getText().toString(); 
      startPhoneNumberVerification(num);     // call function for receive OTP 6 digit code 
     } 
    }); 





    Verify.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String code=Code.getText().toString(); 
      verifyPhoneNumberWithCode(mVerificationId,code);     //call function for verify code 

     } 
    }); 
} 







private void startPhoneNumberVerification(String phoneNumber) { 
    // [START start_phone_auth] 
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
      phoneNumber,  // Phone number to verify 
      60,     // Timeout duration 
      TimeUnit.SECONDS, // Unit of timeout 
      this,    // Activity (for callback binding) 
      mCallbacks);  // OnVerificationStateChangedCallbacks 
    // [END start_phone_auth] 


} 







private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) { 
    mAuth.signInWithCredential(credential) 
      .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        if (task.isSuccessful()) { 
         // Sign in success, update UI with the signed-in user's information 

         FirebaseUser user = task.getResult().getUser(); 
         Toast.makeText(getApplicationContext(), "sign in successfull", Toast.LENGTH_SHORT).show(); 
         // [START_EXCLUDE] 

         // [END_EXCLUDE] 
        } else { 
         // Sign in failed, display a message and update the UI 

         if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { 
          // The verification code entered was invalid 
          // [START_EXCLUDE silent] 

          // [END_EXCLUDE] 
         } 
         // [START_EXCLUDE silent] 
         // Update UI 

         // [END_EXCLUDE] 
        } 
       } 
      }); 
} 






private void verifyPhoneNumberWithCode(String verificationId, String code) { 
    // [START verify_with_code] 
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); 
    // [END verify_with_code] 
    signInWithPhoneAuthCredential(credential); 
} 










private void callback_verificvation() { 

    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { 

     @Override 
     public void onVerificationCompleted(PhoneAuthCredential credential) { 

      signInWithPhoneAuthCredential(credential); 
     } 

     @Override 
     public void onVerificationFailed(FirebaseException e) { 
      // This callback is invoked in an invalid request for verification is made, 

     } 

     @Override 
     public void onCodeSent(String verificationId, 
           PhoneAuthProvider.ForceResendingToken token) { 
      // The SMS verification code has been sent to the provided phone number, we 
      // now need to ask the user to enter the code and then construct a credential 
      // by combining the code with a verification ID. 


      // Save verification ID and resending token so we can use them later 
      mVerificationId = verificationId; 
      mResendToken = token; 

     } 
    }; 
+0

我已經解決了我的問題,並感謝您的回答。你不覺得這只是Firebase本身提供的文檔副本。 –

+0

查看我的解決方案https://stackoverflow.com/a/44966449/7672400 –