如何为 Firebase (Kotlin) 创建自定义身份验证

How Can I Create Custom Auth For Firebase (Kotlin)

我想使用用户名、p 密码和电子邮件将用户添加到 Firebase。我正在使用电子邮件密码登录方法,但如何将用户名添加到数据库中? 这是我为电子邮件和密码登录方法编写的基本代码:


    fun signup(view:View){
        val email = binding.emailText.text.toString()
        val username = binding.usernameText2.text.toString()
        val password = binding.passwordText2.text.toString()
        if(email.equals("") || username.equals("") || password.equals("")){
            Toast.makeText(this,"Don't Leave them empty!",Toast.LENGTH_LONG).show()
        }else{
            auth.createUserWithEmailAndPassword(email,password).addOnSuccessListener {
                //sucess
                val intent = Intent(this@SignupActivity,FeedsActivity::class.java)
                startActivity(intent)
                finish()
            }.addOnFailureListener {
                //failed
                Toast.makeText(this,it.localizedMessage,Toast.LENGTH_LONG).show()
            }

        }

    }

为了向数据库中添加一些数据,您必须选择要使用的数据库。您可以使用 Cloud Firestore, or the Realtime Database. Now, since the sign-in operation is asynchronous, the code that writes data to the database should be added inside the callback. That being said, get the data from the FirebaseUser 对象:

val firebaseUser = FirebaseAuth.getInstance().currentUser
val email = firebaseUser?.email
val displayName = firebaseUser?.displayName
val user = mapOf(
    displayName to displayName,
    email to email
)

并将其添加到 Firestore:

val db = FirebaseFirestore.getInstance()
val usersRef = db.collection("users")
usersRef.document(uid).set(user)

或在实时数据库中:

val db = FirebaseDatabase.getInstance().reference
val usersRef = db.child("users")
usersRef.child(uid).setValue(user)