简体   繁体   English

如何在 Android 的 Firebase 身份验证中更改登录电话号码?

[英]How to change the Signed in Phone Number in Firebase Authentication for Android?

I am using Android Firebase Auth Ui for providing Phone Number based sign in option in an android app.我正在使用Android Firebase Auth Ui在 Android 应用程序中提供基于电话号码的登录选项。 I am trying to provide an additional option to signed in users to switch their signed in phone number to another number keeping the same user account.我试图为登录用户提供一个额外的选项,将他们的登录电话号码切换到另一个保持相同用户帐户的号码。

But as per Firebase Docs for Phone number there are no options to change the signed in number.但是根据电话号码的 Firebase Docs,没有更改登录号码的选项。

There are options for linking different auth providers like email, google or Facebook login etc to same account.可以选择将不同的身份验证提供商(如电子邮件、谷歌或 Facebook 登录等)链接到同一帐户。 But there is no way mentioned about how to change the phone number or email id keeping the same user id.但是没有提到如何更改电话号码或电子邮件 ID 保持相同的用户 ID。

Is there a workaround or method by which we can achieve this?是否有解决方法或方法可以实现这一目标?

存在用于更新当前用户电话号码的 API: FirebaseUser#updatePhoneNumber(PhoneAuthCredential credential)

    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")
        }

    }

I also had this challenge to update user phone number and when I go on documentation I got something by using I have done this task.我也遇到了更新用户电话号码的挑战,当我继续编写文档时,我通过使用我已经完成了这项任务得到了一些东西。

you can go for documentation by click here您可以点击此处获取文档

Now the method you can use : - for java android project.现在您可以使用的方法: - 用于 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
            }
        }
    }
);

Try this尝试这个

//Send otp to phone number

String verificationId;
private void startLoginFirebase(){
    PhoneAuthProvider.getInstance(firebaseAuth).verifyPhoneNumber(phone, 90L, TimeUnit.SECONDS, PhoneAuthActivity.this, new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            verificationId = s;
            updatePhoneNum();
        }

        @Override
        public void onCodeAutoRetrievalTimeOut(@NonNull String s) {
            super.onCodeAutoRetrievalTimeOut(s);
        }

        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {
            processFurther(e.getLocalizedMessage().toString(), 0);
        }
    });
}


//Verify Otp

private void updatePhoneNum(){
    PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, otp);
    firebaseAuth.getCurrentUser().updatePhoneNumber(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

        }
    });
}

显然 - 根据项目维护者的说法 - FirebaseUI-Android不支持此功能,而且他们似乎没有计划在近期内这样做:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM