Firebase手机身份验证凭证与Firebase中的Google登录链接
我已经在我的应用中使用Firebase身份验证实施了两步验证,其中我使用了Gmail,Facebook或简单的电子邮件登录进行身份验证。 由于数字电话验证已迁移到Firebase,我已通过将现有登录帐户(脸书,Gmail或电子邮件)与电话身份验证凭证关联起来实现了Firebase电话身份验证。 使用Facebook和电子邮件帐户时,它可以正常工作。 当用户通过google登录并尝试通过电话身份验证验证移动时,会显示以下日志:
signInWithCredential:失败
com.google.firebase.auth.FirebaseAuthUserCollisionException:一个帐户已经存在,但具有相同的电子邮件地址但登录凭据不同。 使用与此电子邮件地址关联的提供商登录。
阅读这篇文章。 这与文章中提到的问题是否相同? 有没有相同的解决方案..
经过对互联网和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
传递给上述方法,即可验证手机分配给您的现有帐户。 确保它没有被任何其他帐户使用。
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
现在电话验证可在firebase.Here是代码电话Auth使用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中使用的电子邮件相同的Google帐户未关联在一起。 默认情况下,Firebase不允许多次使用同一封电子邮件发生此冲突。
要解决这个问题,你有两个选择
1.将谷歌账户链接到Facebook并发送电子邮件
mAuth.getCurrentUser().linkWithCredential(credential);
将新凭据添加到现有登录用户。
2.从Firebase控制台启用同一封电子邮件中的多个帐户(不推荐)
这将使谷歌登录用户的新uid和以前的Facebook登录用户将有旧的。
链接地址: http://www.djcxy.com/p/32337.html上一篇: Firebase Phone Authentication credentials linking with Google login in firebase