Firebase电话身份验证凭据与firebase

时间:2017-06-14 10:24:47

标签: android authentication firebase firebase-authentication

我已经在我的应用中使用firebase身份验证实现了两步身份验证,其中我使用gmail,facebook或简单的电子邮件登录进行身份验证。由于数字电话验证已迁移到firebase,我已通过将现有登录帐户(Facebook,gmail或电子邮件)与电话身份验证凭据相关联来实施firebase电话身份验证。与Facebook和电子邮件帐户一起使用时,它工作正常。当用户通过谷歌登录并尝试通过电话验证验证移动设备时,将打印此日志:

  

signInWithCredential:失败

     

com.google.firebase.auth.FirebaseAuthUserCollisionException:已存在具有相同电子邮件地址但登录凭据不同的帐户。使用与此电子邮件地址关联的提供商登录。

阅读此article。这是article中提到的问题吗?是否有相同的解决方案..

3 个答案:

答案 0 :(得分:4)

在通过互联网和firebase文档本身进行研究之后,我在应用程序中使用firebase auth找到了这个两步验证的解决方案。

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);

答案 1 :(得分:0)

现在可以在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;

        }
    };

答案 2 :(得分:-1)

此异常被抛出,因为您已将电子邮件pw登录和Facebook登录连接在一起,使用与facebook one中使用的相同电子邮件的Google帐户未关联在一起。默认情况下,firebase不允许同一电子邮件中的多个帐户发生此冲突。

要解决此问题,您有两个选择

<强> 1。将Google帐户与Facebook相关联,然后使用

通过电子邮件发送电子邮件
mAuth.getCurrentUser().linkWithCredential(credential); 

向现有登录用户添加新凭据。

<强> 2。从firebase控制台启用来自同一电子邮件(不推荐)的多个帐户

这将为谷歌登录用户创建新的uid,之前的Facebook登录用户将拥有旧的。