2017-06-14 25 views
5

Tôi đã triển khai xác thực hai bước trong ứng dụng của mình bằng xác thực Firebase mà tôi đã sử dụng gmail, facebook hoặc đăng nhập email đơn giản để xác thực. Khi xác minh số điện thoại đã di chuyển sang firebase tôi đã thực hiện xác thực điện thoại firebase bằng cách liên kết tài khoản đã đăng nhập hiện có (facebook, gmail hoặc email) với thông tin xác thực điện thoại. Nó hoạt động tốt khi được sử dụng với facebook và tài khoản email. Khi người dùng đăng nhập thông qua google và cố gắng kiểm tra điện thoại di động thông qua xác thực điện thoại bản ghi này được in:Thông tin xác thực điện thoại Firebase liên kết với thông tin đăng nhập Google trong firebase

signInWithCredential: thất bại

com.google.firebase.auth.FirebaseAuthUserCollisionException: Tài khoản đã tồn tại với cùng địa chỉ email nhưng thông tin đăng nhập khác nhau. Đăng nhập bằng nhà cung cấp được liên kết với địa chỉ email này.

Đọc này article. Có phải vấn đề giống như đã đề cập trong số article ?? Có giải pháp nào giống nhau không ..

Trả lời

-1

Ngoại lệ này được ném vì bạn đã kết nối thông tin đăng nhập email và đăng nhập facebook với nhau, tài khoản google có cùng email được sử dụng trong facebook không được liên kết với nhau. Theo mặc định, firebase không cho phép nhiều tài khoản từ cùng một email xảy ra xung đột này.

Để giải quyết vấn đề này, bạn có hai lựa chọn

1. Liên kết tài khoản google đến facebook và gửi email cho một đang sử dụng

mAuth.getCurrentUser().linkWithCredential(credential); 

Thêm chứng chỉ mới cho hiện có người dùng đăng nhập.

2. Enable nhiều tài khoản từ cùng một email (không khuyến khích) từ giao diện điều khiển căn cứ hỏa lực

Điều này sẽ làm uid mới cho google đăng nhập người dùng và người dùng facebook đăng nhập trước đó sẽ có một tuổi.

+0

Vui lòng đọc kỹ câu hỏi. Tôi đã thực hiện chức năng 'linkWithCredential() 'với tất cả các cài đặt được hỗ trợ và tôi không muốn tạo nhiều tài khoản. Tôi gặp sự cố với xác thực điện thoại. –

1

Sau khi nghiên cứu qua internet và trong tài liệu về Firebase, tôi đã tìm thấy giải pháp cho xác thực hai bước này trong ứng dụng bằng cách sử dụng autbase 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(); 
       } 
      } 
     } 
    }); 

Chỉ cần vượt qua phương thức PhoneAuthCredential trở lên và nó sẽ xác minh số điện thoại được gán cho tài khoản hiện tại của bạn. Hãy chắc chắn rằng nó không được sử dụng bởi bất kỳ tài khoản khác.

PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); 
0

bây giờ điện thoại auth có sẵn trong firebase.Here là Mã cho điện thoại Auth sử dụng căn cứ hỏa lực: Nếu có bất kỳ câu hỏi Phí tự do ASK tôi.

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

Tôi đã giải quyết được vấn đề của mình và cảm ơn câu trả lời. Bạn không nghĩ rằng đây chỉ là bản sao của tài liệu được cung cấp bởi chính firebase. –

+0

kiểm tra giải pháp của tôi https://stackoverflow.com/a/44966449/7672400 –

Các vấn đề liên quan