简体   繁体   English

Firebase Android - 在Kotlin中使用电子邮件和密码创建用户

[英]Firebase Android — create user with email and Password in Kotlin

I'm trying to do a registration with Firebase and Kotlin. 我正在尝试向Firebase和Kotlin进行注册。 Taking a look to the docs, I see all the examples in Java. 看一下docs,我看到了Java中的所有例子。 So when I try to implement in Kotlin I'm not able to make it work. 因此,当我尝试在Kotlin中实施时,我无法使其工作。

In Java is supposed to be like: 在Java中应该是这样的:

// [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: 但是当我尝试在kotlin中实现这样的时候:

// [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: 但是这个,给我一个错误: 在此输入图像描述

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 该示例来自: https//github.com/firebase/quickstart-android/blob/master/auth/app/src/main/java/com/google/firebase/quickstart/auth/EmailPasswordActivity.java#L119-L137

I have implemented Firebase registration with email and password in the following way and it works: 我已通过以下方式使用电子邮件和密码实施Firebase注册,它可以正常工作:

this.firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task: Task<AuthResult> ->
    if (task.isSuccessful) {
        //Registration OK
        val firebaseUser = this.firebaseAuth.currentUser!!
    } else {
        //Registration error 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM