Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Android -- create user with email and Password in Kotlin

I'm trying to do a registration with Firebase and Kotlin. Taking a look to the docs, I see all the examples in Java. So when I try to implement in Kotlin I'm not able to make it work.

In Java is supposed to be like:

// [START create_user_with_email]
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            FirebaseUser user = mAuth.getCurrentUser();

                        } else {
                            // If sign in fails, display a message to the user.
                            ......
                        }

                        // [START_EXCLUDE]
                        .......
                        // [END_EXCLUDE]
                    }
                });
        // [END create_user_with_email]

But when I try to implement in kotlin like this:

// [START create_user_with_email]
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, OnCompleteListener<AuthResult> { task ->
                    if (task.isSuccessful) {
                        // Sign in success, update UI with the signed-in user's information                        
                        val user = mAuth.currentUser                       
                    } else {
                        ......
                    }

                    // [START_EXCLUDE]
                            .....
                    // [END_EXCLUDE]
                })
        // [END create_user_with_email]

But this, give me an error: enter image description here

And I don't know how to solve it.

The example is from: https://github.com/firebase/quickstart-android/blob/master/auth/app/src/main/java/com/google/firebase/quickstart/auth/EmailPasswordActivity.java#L119-L137

like image 933
Shudy Avatar asked Dec 02 '22 11:12

Shudy


1 Answers

I have implemented Firebase registration with email and password in the following way and it works:

this.firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task: Task<AuthResult> ->
    if (task.isSuccessful) {
        //Registration OK
        val firebaseUser = this.firebaseAuth.currentUser!!
    } else {
        //Registration error 
    }
}
like image 54
AlexTa Avatar answered Dec 04 '22 13:12

AlexTa