首页 > 解决方案 > 将数据发送到firestore 数据库| 活动 | 科特林

问题描述

我想让用户数据在注册时发送到 Firestore 数据库。注册机制我已经做好了,但是不知道怎么把这个数据发送到数据库。不幸的是,我在任何地方都找不到确切的答案或代码:(

我的注册活动:

    class RegisterActivity : AppCompatActivity() {
    
        private val mailAuth = FirebaseAuth.getInstance()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_register)
    
            findViewById<TextView>(R.id.tv_login).setOnClickListener {
    
                startActivity(Intent(this@RegisterActivity, LoginActivity::class.java))
                finish()
            }
    
                    findViewById<TextView>(R.id.tv_login).setOnClickListener {

            startActivity(Intent(this@RegisterActivity, LoginActivity::class.java))
            finish()
        }

        findViewById<Button>(R.id.btn_register).setOnClickListener {
            when {
                TextUtils.isEmpty(findViewById<EditText>(R.id.et_register_email).text.toString().trim { it <= ' ' }) -> {
                    Toast.makeText(
                        this@RegisterActivity,
                        "Please enter email.",
                        Toast.LENGTH_SHORT
                    ).show()
                }

                TextUtils.isEmpty(findViewById<EditText>(R.id.et_register_password).text.toString().trim { it <= ' ' }) -> {
                    Toast.makeText(
                        this@RegisterActivity,
                        "Please enter password.",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                else -> {

                    val email: String = findViewById<EditText>(R.id.et_register_email).text.toString().trim { it <= ' ' }
                    val password: String = findViewById<EditText>(R.id.et_register_password).text.toString().trim { it <= ' ' }

                    FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(


                            OnCompleteListener<AuthResult> { task ->

                                if (task.isSuccessful) {


                                    val rootRef = FirebaseFirestore.getInstance()
                                    val usersRef = rootRef.collection("users")
                                    val auth = FirebaseAuth.getInstance()
                                    auth.currentUser?.apply {
                                        usersRef.document(uid).set(mapOf(
                                            "uid" to uid,
                                            "email" to email
                                        )).addOnCompleteListener{ task ->
                                            if (task.isSuccessful) {
                                                Log.d("TAG", "User successfully added.")
                                            } else {
                                                Log.d("TAG", task.exception!!.message!!)
                                            }
                                        }
                                    }


                                    val firebaseUser: FirebaseUser = task.result!!.user!!


                                    Log.d(
                                        "RegisterActivity", "register-in"
                                    )
                                    val intent = Intent(this@RegisterActivity, NavigationActivity::class.java)
                                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                                    intent.putExtra("user_id", firebaseUser.uid)
                                    intent.putExtra("email_id", email)
                                    startActivity(intent)
                                    finish()
                                } else {
                                    Toast.makeText(
                                        this@RegisterActivity,
                                        task.exception!!.message.toString(),
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                            }
                        )
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        isCurrentUser()
    }
    private fun isCurrentUser() {
        mailAuth.currentUser?.let {
            val intent = Intent(applicationContext, NavigationActivity::class.java).apply {
                flags = (Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
            }
            startActivity(intent)
        }

    }
}

标签: androidfirebasekotlingoogle-cloud-firestore

解决方案


为了能够将用户数据写入 Firestore,您必须确保用户已通过身份验证。所以请添加以下代码行:

val rootRef = FirebaseFirestore.getInstance()
val usersRef = rootRef.collection("users")
val auth = FirebaseAuth.getInstance()
auth.currentUser?.apply {
    usersRef.document(uid).set(mapOf(
        "uid" to uid,
        "email" to email
    )).addOnCompleteListener{ task ->
        if (task.isSuccessful) {
            Log.d("TAG", "User successfully added.")
        } else {
            Log.d("TAG", task.exception!!.message!!)
        }
    }
}

就在里面:

if (task.isSuccessful) {
    ...
}

logcat 中的结果将是:

Firestore-root
  |
  --- users (collection)
       |
       --- $uid (document)
            |
            --- uid: "The uid that comes from the authentication process"
            |
            --- email: "The email address used to authenticate"

推荐阅读