如何更改Android版Firebase身份验证中的签名电话号码?

时间:2017-06-17 06:36:59

标签: android authentication firebase firebase-authentication

我正在使用Android Firebase Auth Ui在Android应用中提供基于电话号码的登录选项。我正在尝试为登录用户提供一个额外选项,以将其登录的电话号码切换到另一个保留相同用户帐户的号码。

但根据Firebase Docs for Phone number,没有更改已登录号码的选项。

可以选择将不同的身份验证提供商(例如电子邮件,Google或Facebook登录等)链接到同一帐户。但是没有办法提到如何更改保持相同用户ID的电话号码或电子邮件ID。

是否有解决方法或方法可以实现此目标?

4 个答案:

答案 0 :(得分:6)

答案 1 :(得分:1)

我也遇到了更新用户电话号码的挑战,当我继续进行文档记录时,我已经通过完成此任务得到了一些东西。

您可以通过单击here

来获取文档

现在可以使用的方法:-用于Java android项目。

PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential( "+91-98298XXXX2", "OTP_CODE" );
// Update Mobile Number...
firebaseAuth.getCurrentUser().updatePhoneNumber(phoneAuthCredential)
    .addOnCompleteListener(new OnCompleteListener <Void>() {
        @Override
        public void onComplete(@NonNull Task <Void> task) {
            if (task.isSuccessful()) {
                // Update Successfully
            } else {
                // Failed
            }
        }
    }
);

答案 2 :(得分:1)

    val options = PhoneAuthOptions.newBuilder(FirebaseAuth.getInstance())
            .setPhoneNumber(phoneNumber) // Phone number to verify
            .setTimeout(100L, TimeUnit.SECONDS) // Timeout and unit
            .setActivity(activity) // Activity (for callback binding)
            .setCallbacks(returnCallBack()) // OnVerificationStateChangedCallbacks
            .build()

 PhoneAuthProvider.verifyPhoneNumber(options)

private fun returnCallBack() = object : PhoneAuthProvider
    .OnVerificationStateChangedCallbacks() {

        override fun onVerificationCompleted(credential: PhoneAuthCredential) {
            FirebaseAuth.getCurrentUser()?.updatePhoneNumber(credential)
        }

        override fun onVerificationFailed(e: FirebaseException) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            Log.e("phone",  e.toString())

        }

        override fun onCodeSent(verificationId: String, token: PhoneAuthProvider.ForceResendingToken) {
            //You need this to pass as a parameter for the update method call.
            vericationSent = verificationId
        }
    }   

 fun confirmChange(code: String, context: Context?) {
        if(code.contains(Regex(onlyNumber))) {
            Log.d("codeSent" , "Right code : $code")

            FirebaseAuth.getCurrentUser()
                ?.updatePhoneNumber(PhoneAuthProvider.getCredential(vericationSent, code))
                ?.addOnCompleteListener {task ->
                    //it worked if you reach here.

                }?.addOnFailureListener {
                    //Show the error to user
                    }
                }

            vericationSent = EMPTY
        } else {
            Log.d("codeSent" , "wrong code : $code")
        }

    }

答案 3 :(得分:0)

显然-根据项目维护者的说法-FirebaseUI-Android doesn't support this feature,看来他们没有任何计划在不久的将来:(