这是我的注册活动。当按下按钮时,它会注册用户,但不会将其数据存储在数据库中。它显示用户在 firebase 控制台的身份验证选项卡中注册,但数据库中没有数据。这是我对该应用程序的注册活动。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_signup)
val signin_btn = findViewById<Button>(R.id.signin_link_btn)
val signup_btn = findViewById<Button>(R.id.signup_btn)
signin_btn.setOnClickListener {
startActivity(Intent(this, LoginActivity::class.java))
}
signup_btn.setOnClickListener{
createAccount()
}
}
private fun createAccount() {
val fullName = findViewById<EditText>(R.id.fullname_signup).text.toString()
val userName = findViewById<EditText>(R.id.username_signup).text.toString()
val email = findViewById<EditText>(R.id.email_signup).text.toString()
val password = findViewById<EditText>(R.id.password_signup).text.toString()
when {
TextUtils.isEmpty(fullName) -> Toast.makeText(this, "Full Name is Required", Toast.LENGTH_LONG).show()
TextUtils.isEmpty(userName) -> Toast.makeText(this, "UserName is Required", Toast.LENGTH_LONG).show()
TextUtils.isEmpty(email) -> Toast.makeText(this, "Email is Required", Toast.LENGTH_LONG).show()
TextUtils.isEmpty(password) -> Toast.makeText(this, "Password is Required", Toast.LENGTH_LONG).show()
else -> {
val mAuth: FirebaseAuth = FirebaseAuth.getInstance()
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener{
task ->
if (task.isSuccessful)
{
saveUserInfo(fullName, userName, email)
}
else
{
val message = task.exception!!.toString()
Toast.makeText(this, "Error: $message", Toast.LENGTH_LONG).show()
mAuth.signOut()
}
}
}
}
}
private fun saveUserInfo(fullName: String, userName: String, email: String) {
val currentUserID= FirebaseAuth.getInstance().currentUser!!.uid
val usersRef: DatabaseReference = FirebaseDatabase.getInstance().reference.child("Users")
val userMap = HashMap<String, Any>()
userMap["uid"] = currentUserID
userMap["fullname"] = currentUserID
userMap["username"] = currentUserID
userMap["email"] = currentUserID
userMap["bio"] = "Hey I am using Instagram Clone App."
userMap["image"] = "https://firebasestorage.googleapis.com/v0/b/instagram-clone-565fe.appspot.com/o/Instagram%20Clonde%20Default%20Images%2Fprofile.png?alt=media&token=494e98e8-8e7d-40ea-8102-97849ac546b7"
usersRef.child("Users").child(currentUserID).setValue(userMap).addOnCompleteListener {
task ->
if(task.isSuccessful){
Toast.makeText(this,"Account has been created Successfully.", Toast.LENGTH_SHORT).show()
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
}
else
{
val message = task.exception!!.toString()
Toast.makeText(this, "Error: $message", Toast.LENGTH_LONG).show()
FirebaseAuth.getInstance().signOut()
}
}
}
}
我的数据库的规则是
{
"rules": {
".read": "true", // 2021-8-5
".write": "true ", // 2021-8-5
}
}
这里是项目的gradle依赖
依赖项{
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'com.google.firebase:firebase-auth:21.0.1'
implementation 'com.google.firebase:firebase-database:20.0.0'
implementation 'com.google.firebase:firebase-storage:20.0.0'
implementation 'com.google.firebase:firebase-analytics:19.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}