[英]Firebase Phone Authentication credentials linking with Google login in firebase
我已經在我的應用程序中使用firebase身份驗證實現了兩步身份驗證,其中我使用gmail,facebook或簡單的電子郵件登錄進行身份驗證。 由於數字電話驗證已遷移到firebase,我已通過將現有登錄帳戶(Facebook,gmail或電子郵件)與電話身份驗證憑據相關聯來實施firebase電話身份驗證。 與Facebook和電子郵件帳戶一起使用時,它工作正常。 當用戶通過谷歌登錄並嘗試通過電話驗證驗證移動設備時,將打印以下日志:
signInWithCredential:失敗
com.google.firebase.auth.FirebaseAuthUserCollisionException:已存在具有相同電子郵件地址但登錄憑據不同的帳戶。 使用與此電子郵件地址關聯的提供商登錄。
在通過互聯網和firebase文檔本身進行研究之后,我使用firebase auth在app中找到了這個兩步驗證的解決方案。
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
傳遞給上述方法,它就會驗證手機是否已分配給您現有的帳戶。 確保任何其他帳戶都不使用它。
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
現在可以在firebase中使用手機驗證。這是使用Firebase進行電話驗證的代碼:如果有任何問題費用請問我。
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;
}
};
之所以拋出此異常,是因為您已將電子郵件pw登錄和facebook登錄連接在一起,使用facebook one中使用的相同電子郵件的Google帳戶未關聯在一起。 默認情況下,firebase不允許來自同一電子郵件的多個帳戶發生此沖突。
要解決此問題,您有兩種選擇
1.將谷歌帳戶鏈接到Facebook並使用電子郵件發送給我們
mAuth.getCurrentUser().linkWithCredential(credential);
將新憑據添加到現有登錄用戶。
2.從firebase控制台啟用來自同一電子郵件(不推薦)的多個帳戶
這將為谷歌登錄用戶創建新的uid,之前的Facebook登錄用戶將擁有舊的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.