android - 将数据发送到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)
}
}
}
解决方案
为了能够将用户数据写入 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"
推荐阅读
- android - 为相机意图创建 URI 时应用程序崩溃
- android - 适用于 Android 的 Fabric 插件中的每日新用户身份 - crashlytics
- java - 没有模块化的 Java 11:包在作为 maven 依赖项添加时不存在
- sql - 没有返回行时使用 COALESCE 与 EXCEPTION (Oracle)
- javascript - 是否可以从 nodejs 获取衍生进程的进度?
- django - django_rest_framework,如何通过令牌更新用户配置文件?
- post - Http Post Indy10 错误 Delphi 7:创建 SSL 上下文时出错
- django - DRF JSONRenderer 'ReturnList' 对象没有属性 'get'
- xamarin - 如何在 xamarin 表单中在横向模式和纵向模式下使图标大小相同
- python - ipdb:RuntimeWarning:导入包“ipdb”后在 sys.modules 中找到“ipdb.__main__”