为什么我无法将多个验证提供商链接到Android Jetpack上的Firebase帐户?

发布于 2025-02-10 18:53:58 字数 2904 浏览 0 评论 0 原文

我在JetPack项目中有登录项目,因此,我想在项目中也使用电话身份验证,因此,当我完成流程时,在Firebase提供商部分,电子邮件和电话号码没有链接,我读到 example 问题,我在项目中添加了这一行代码,

  val credential = PhoneAuthProvider.getCredential(verificationOtp, otp)
  auth.currentUser?.linkWithCredential(credential)
  FirebaseAuth.getInstance().signInWithCredential(credential)

但仍然不起作用,有什么想法吗?

@HiltViewModel
class AuthenticationViewModel @Inject constructor(

    private val auth: FirebaseAuth

    ) : ViewModel() {

    lateinit var otp: String

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

    private lateinit var baseBuilder: PhoneAuthOptions.Builder

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

    lateinit var resendToken: PhoneAuthProvider.ForceResendingToken

    fun send(mobileNum: String) {

        val options = baseBuilder
            .setPhoneNumber(mobileNum)
            .setTimeout(5L, 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 signInWithPhoneAuthCredential(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")

                }
            }
    }


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


}

I have sign in project in jetpack project so, I want to use phone authentication also in my project, so when I complete my process, in firebase providers section, my email and phone number not link, I read example question, and I added this line of code in my project

  val credential = PhoneAuthProvider.getCredential(verificationOtp, otp)
  auth.currentUser?.linkWithCredential(credential)
  FirebaseAuth.getInstance().signInWithCredential(credential)

but still it is not work, any idea?

@HiltViewModel
class AuthenticationViewModel @Inject constructor(

    private val auth: FirebaseAuth

    ) : ViewModel() {

    lateinit var otp: String

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

    private lateinit var baseBuilder: PhoneAuthOptions.Builder

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

    lateinit var resendToken: PhoneAuthProvider.ForceResendingToken

    fun send(mobileNum: String) {

        val options = baseBuilder
            .setPhoneNumber(mobileNum)
            .setTimeout(5L, 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 signInWithPhoneAuthCredential(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")

                }
            }
    }


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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文