Coroutine Freeze on Google Auth Sign In

I seem to be having issues implementing coroutines into my jetpack compose android app. It freezes on .await(). Code:

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LoginScreen(navController: NavController) {
    lateinit var auth: FirebaseAuth

    auth = FirebaseAuth.getInstance()

    auth.signOut()

        //More UI in between

        Button(
            //UI
            onClick = {
                val email = userState.toString()
                val password = passwordState.toString()

                if(email.isNotEmpty() && password.isNotEmpty()){

                    //CALL HERE
                    runBlocking { signInUser(auth, email, password) }
                    if(auth.currentUser != null){
                        navController.navigate("HomeScreen/$email/$password")
                    }



                }

            }
        )
}

Here is the called function where it freezes on the .await():

suspend fun signInUser(auth: FirebaseAuth, email: String, password: String) {
    auth.signInWithEmailAndPassword(email, password).await()
}

Dependencies, if this might be a version issue:

dependencies {

    implementation("com.google.firebase:firebase-auth-ktx:22.1.2")
    implementation("androidx.constraintlayout:constraintlayout-compose:1.0.1")
    implementation("com.google.firebase:firebase-firestore-ktx:24.8.1")
    val nav_version = "2.7.2"

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.1")
    implementation("androidx.compose.material:material-icons-extended:1.5.1")
    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
    implementation("androidx.activity:activity-compose:1.7.0")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.navigation:navigation-compose:$nav_version")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}

Thanks. Hope I included enough info.

I expect the .await() function to sign in the user and move on after signing in, instead it just freezes on the .await() and quits responding. I’ve mainly just been trying to get auth to hold its logged-in state between screens and figured coroutines were the best way to go. It doesn’t work when I declare it within the UI and add a listener like in the google docs, going to a different screen wipes out the state.

val user = Firebase.auth.currentUser 

Always returns null.

  • 2

    You really shouldn’t block the main thread at all on asynchronous calls since you have no guarantee how long they will take to complete. Instead, learn async programming with the usual tools like livedata and coroutines that offload blocking work onto other threads.

    – 

  • I suggest you go through the following link kotlinlang.org/docs/coroutines-basics.html, runBlocking is not ideal for doing async operation in UI thread

    – 

Leave a Comment