Firebase电话身份验证凭证链接谷歌登录在Firebase
我已经在我的应用程序中使用Firebase身份验证实施了两步身份验证,其中我使用了Gmail,Facebook或简单的电子邮件登录进行身份验证。由于数字电话验证已迁移到Firebase,我已通过将现有登录帐户(脸书,Gmail或电子邮件)与电话身份验证凭证关联起来实现了Firebase电话身份验证。使用Facebook和电子邮件帐户时,它可以正常工作。当用户通过谷歌登录并试图通过电话身份验证来验证移动这个日志印刷:Firebase电话身份验证凭证链接谷歌登录在Firebase
signInWithCredential:失败
com.google.firebase.auth.FirebaseAuthUserCollisionException:一个帐户已使用相同的存在电子邮件地址,但不同的登录凭据。使用与此电子邮件地址关联的提供商登录。
此异常是因为您已连接电子邮件登录和Facebook登录在一起,具有相同的电子邮件中使用的Facebook帐户的谷歌帐户没有关联在一起。默认情况下,Firebase不允许多次使用同一封电子邮件发生此冲突。
为了解决这个问题,你有两个选择
1.链接的谷歌帐户的Facebook和电子邮件一个人的使用
mAuth.getCurrentUser().linkWithCredential(credential);
添加新的凭证到现有的登录用户。
2.从火力控制台
启用从相同的电子邮件(不推荐)多帐户这将使新的UID为谷歌用户登录和之前的Facebook登录的用户将有旧的。
经过互联网和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);
现在手机权威性是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;
}
};
我已经解决了我的问题,并感谢您的回答。你不觉得这只是Firebase本身提供的文档副本。 –
查看我的解决方案https://*.com/a/44966449/7672400 –
请仔细阅读问题。我已经使用所有支持的设置实现了'linkWithCredential()'功能,并且我不想创建多个帐户。我的手机身份验证有问题。 –