我们如何 link 为 phone 和 Jetpack 中的电子邮件撰写多个身份验证提供程序?

How we can link multiple authentication providers for phone and email in jetpack compose?

我的注册应用程序中有邮件和移动身份验证,在 firebase 中,当用户使用邮件和移动设备注册时 phone 它为同一用户生成两个不同的 UID,我希望实现的是一个用户一个UID可以用邮箱登录,也可以用phone号登录(认证时合并phone号和邮箱)。我可以在 Firebase 实现吗?我有示例 ,但我不明白如何实现 Jetpack Compose,知道吗?

视图模型phone:

@HiltViewModel
class AuthenticationViewModel @Inject constructor(


    ) : ViewModel() {

    private val mAuth = FirebaseAuth.getInstance()

    var verificationOtp = ""
    var popNotification = mutableStateOf<Event<String>?>(null)

 private lateinit var baseBuilder: PhoneAuthOptions.Builder

fun setActivity(activity: Activity) {
    baseBuilder = 
PhoneAuthOptions.newBuilder().setActivity(activity)

}



    fun send(mobileNum: String) {
        val options = baseBuilder
            .setPhoneNumber("+91$mobileNum")
            .setTimeout(60L, TimeUnit.SECONDS)
            .setCallbacks(object :
                PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                override fun onVerificationCompleted(p0: PhoneAuthCredential) {
                    handledException(customMessage = "Verification Completed")

                }

                override fun onVerificationFailed(p0: FirebaseException) {
                    handledException(customMessage = "Verification Failed")

                }

                override fun onCodeSent(otp: String, p1: PhoneAuthProvider.ForceResendingToken) {
                    super.onCodeSent(otp, p1)
                    verificationOtp = otp
                    handledException(customMessage = "Otp Send Successfully")

                }
            }).build()
        PhoneAuthProvider.verifyPhoneNumber(options)
    }

     fun otpVerification(otp: String) {
        val credential = PhoneAuthProvider.getCredential(verificationOtp, otp)
        FirebaseAuth.getInstance().signInWithCredential(credential)
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    handledException(customMessage = "Verification Successful")

                } else {
                    handledException(customMessage =  "Wrong Otp")

                }
            }
    }


    private fun handledException(exception: Exception? = null, customMessage: String = "") {
        exception?.printStackTrace()
        val errorMsg = exception?.message ?: ""
        val message = if (customMessage.isEmpty()) {
            errorMsg
        } else {
            "$customMessage: $errorMsg"
        }
        popNotification.value = Event(message)
    }


    }

当我在 otpVerification 函数中添加 auth.currentUser?.linkWithCredential(credential) 行代码时,我解决了问题。

fun otpVerification(otp: String) {
    val credential = PhoneAuthProvider.getCredential(verificationOtp, otp)
     auth.currentUser?.linkWithCredential(credential)
    FirebaseAuth.getInstance().signInWithCredential(credential)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                handledException(customMessage = "Verification Successful")

            } else {
                handledException(customMessage =  "Wrong Otp")

            }
        }
}