首页 文章

Firebase电话身份验证凭据与firebase中的Google登录链接

提问于
浏览
4

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

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

阅读本文article . 是否与_602147中提到的问题相同?有没有相同的解决方案..

3 回答

  • -1

    在通过互联网和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

    now phone auth is available in firebase.Here is Code for Phone Auth using 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;
    
            }
        };
    
  • 0

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

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

    1. Link the google account to the facebook and email one's using

    mAuth.getCurrentUser().linkWithCredential(credential);
    

    将新凭据添加到现有登录用户 .

    2. Enable multiple account from same email(not recommended) from the firebase console

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

相关问题