Home > Blockchain >  Firebase creates user in database but task is never successfull
Firebase creates user in database but task is never successfull

Time:12-04

User is created in database but "ok" is never true

fun signUp(userEmail: String, userPassword: String, firstName: String, lastName: String):Boolean {

var ok:Boolean= false

val firebaseAuth: FirebaseAuth = FirebaseAuth.getInstance()
firebaseAuth.createUserWithEmailAndPassword(userEmail,userPassword)
    .addOnCompleteListener { task->
        if(task.isSuccessful){
            ok=true
        }
    }
return ok}

}

I'm trying to write mvvm code for login in android studio. Everything works fine but "ok" always remains false and I need it for navigation in another file

CodePudding user response:

(From OpenAI)

There are a few issues with the code you have provided that may be causing the problem you are experiencing.

First, the createUserWithEmailAndPassword method of the FirebaseAuth class is asynchronous, which means that it returns immediately without waiting for the user to be created on the server. This means that the ok variable is returned before the user is actually created, so it will always be false in the current code.

To fix this problem, you can use a callback or a coroutine to wait for the user to be created before returning the ok variable. Here is an example of how you could use a callback to fix this issue:

fun signUp(userEmail: String, userPassword: String, firstName: String, lastName: String, callback: (Boolean) -> Unit) {
    val firebaseAuth: FirebaseAuth = FirebaseAuth.getInstance()
    firebaseAuth.createUserWithEmailAndPassword(userEmail,userPassword)
        .addOnCompleteListener { task->
            if(task.isSuccessful){
                callback(true)
            } else {
                callback(false)
            }
        }
}

CodePudding user response:

The return statement in your code is being executed before the addOnCompleteListener callback is called. This means that the value of ok is always false when the function returns.

To fix this, you can move the return ok statement inside the addOnCompleteListener callback, like this:

fun signUp(userEmail: String, userPassword: String, firstName: String, lastName: String):Boolean {
    val firebaseAuth: FirebaseAuth = FirebaseAuth.getInstance()
    firebaseAuth.createUserWithEmailAndPassword(userEmail,userPassword)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                return true
            } else {
                return false
            }
        }
}

This way, the value of ok will be returned only after the addOnCompleteListener callback is called.

Note: This code still has a problem because the return statements inside the callback will not return from the outer signUp function. To fix this, you can use a lambda with a receiver, like this:

fun signUp(userEmail: String, userPassword: String, firstName: String, lastName: String):Boolean {
    val firebaseAuth: FirebaseAuth = FirebaseAuth.getInstance()
    firebaseAuth.createUserWithEmailAndPassword(userEmail,userPassword)
        .addOnCompleteListener {
            if (it.isSuccessful) return true
            else return false
        }
}

This way, the return statements inside the lambda will return from the outer signUp function.

  • Related