Shudy
Shudy

Reputation: 7936

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

Upvotes: 4

Views: 7540

Answers (2)

Sebastian Soto
Sebastian Soto

Reputation: 1

auth.createUserWithEmailAndPassword(email,pass).addOnCompleteListener(object: OnCompleteListener<AuthResult>{
                        override fun onComplete(p0: Task<AuthResult>) {
                            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                        }
                    })

Upvotes: 0

AlexTa
AlexTa

Reputation: 5251

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 
    }
}

Upvotes: 18

Related Questions