向firestore数据库发送数据| Activity | Kotlin



我想让用户数据被发送到Firestore数据库登记。我已经做了注册机制,但是我不知道如何将这些数据发送到数据库。不幸的是,我找不到关于如何做到这一点的确切答案或代码:(

我RegisterActivity:

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

为了能够将用户数据写入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) {
...
}

日志的结果将是:

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

最新更新