firebasekotlinmvvmfirebase-authenticationandroid-mvvm

How can firebase phone auth be implemented in view model?


I'm trying to add the Firebase Phone Authentication code inside a View Model using Kotlin. The problem is that the PhoneAuthProvider requires an activity. Does anyone know how can this code be implemented inside a View Model without the need of an activity?

Thanks!

val mCallbacks: PhoneAuthProvider.OnVerificationStateChangedCallbacks ...

val options = PhoneAuthOptions.newBuilder(auth).apply {
  setPhoneNumber(phoneNumber)
  setTimeout(120L, TimeUnit.SECONDS)
  setActivity(this) <-------------------------- // Activity (for callback binding)
  setCallbacks(mCallbacks)
}.build()
PhoneAuthProvider.verifyPhoneNumber(options)

Solution

  • It turned out to be an intentional change in API 20 (check out this issue on Github), even though it violates the MVVM architecture. The reason an activity is needed is that the method falls back to reCAPTCHA. The right way to achieve it is "yet to be determined".