Issue
I want to create a signing activity in my app using Firebase phone authentication. The authentication has three phases:
- Sending verification code with
PhoneAuthProvider.verifyPhoneNumber(options)
- Verify the code with
PhoneAuthProvider.getCredential(verificationId!!, code)
- Sign in the user with
auth.signInWithCredential(credential)
I want to use Coroutines to handle the signing process. I know how to handle code verification using await()
, which wrap the Task
returned by auth.signInWithCredential(credential)
.
My problem is with PhoneAuthProvider.verifyPhoneNumber(options)
function which is a void
function. I must use callbacks to handle this method.
val options = PhoneAuthOptions.newBuilder(auth)
.setPhoneNumber(phoneNumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setCallbacks(callback)
.build()
PhoneAuthProvider.verifyPhoneNumber(options)
where callbacks
is:
callbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
Timber.i("onVerificationCompleted:$credential")
signInWithPhoneAuthCredential(credential)
}
override fun onVerificationFailed(e: FirebaseException) {
Timber.i(e,"onVerificationFailed")
}
override fun onCodeSent(verificationId: String, token: PhoneAuthProvider.ForceResendingToken) {
Timber.i("onCodeSent:$verificationId")
storedVerificationId = verificationId
resendToken = token
}
}
The question is: is there a way to use await()
with verifyPhoneNumber
function?
Otherwise, how can I use coroutines with callbacks to block the function until callbacks triggered?
Solution
You can use suspendCoroutine to wrap Firebase callback into a coroutine suspend function like this:
sealed class PhoneAuthResult {
data class VerificationCompleted(val credentials: PhoneAuthCredential) : PhoneAuthResult()
data class CodeSent(val verificationId: String, val token: PhoneAuthProvider.ForceResendingToken)
: PhoneAuthResult()
}
private suspend fun performPhoneAuth(
phoneNumber: String,
firebaseAuth: FirebaseAuth): PhoneAuthResult =
suspendCoroutine { cont ->
val callback = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
Timber.i("onVerificationCompleted:$credential")
cont.resume(
PhoneAuthResult.VerificationCompleted(credential)
)
}
override fun onVerificationFailed(e: FirebaseException) {
Timber.i(e, "onVerificationFailed")
cont.resumeWithException(e)
}
override fun onCodeSent(verificationId: String, token: PhoneAuthProvider.ForceResendingToken) {
Timber.i("onCodeSent:$verificationId")
cont.resume(
PhoneAuthResult.CodeSent(verificationId, token)
)
}
}
val options = PhoneAuthOptions.newBuilder(firebaseAuth)
.setPhoneNumber(phoneNumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setCallbacks(callback)
.build()
PhoneAuthProvider.verifyPhoneNumber(options)
}
Answered By - Julián Falcionelli
Answer Checked By - Willingham (JavaFixing Volunteer)