[英]Firebase authentication with email and password extra fields duplicating - Android - Java
我正在嘗試使用 email 和密碼對用戶進行身份驗證,當有人要注冊時,我想添加兩個額外的字段(名稱和 kredi)。 但是當我注冊我的應用程序時,另外兩個帶有“用戶”標簽的字段將保存到 firebase 實時數據庫中,如下所示:
wohR9lJXnMgj9zCYMaasZDctrhf1
kredi:
"20"
name:
"test"
userKredi:
"20"
userName:
"test"
我不明白為什么會這樣。 這是我的注冊活動代碼:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class SignUpActivity extends AppCompatActivity {
EditText signUpMail,signUpPass,kullaniciAdi;
Button signUpButton;
private FirebaseAuth auth;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
auth=FirebaseAuth.getInstance();
signUpMail = findViewById(R.id.signUpMail);
signUpPass = findViewById(R.id.signUpPass);
kullaniciAdi = findViewById(R.id.kullaniciAdi);
auth=FirebaseAuth.getInstance();
signUpButton = findViewById(R.id.signUpButton);
signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signUp();
}
});
}
private void signUp() {
final String email = signUpMail.getText().toString().trim();
final String pass = signUpPass.getText().toString().trim();
final String kredi = "20";
final String name = kullaniciAdi.getText().toString().trim();
if(TextUtils.isEmpty(email)){
Toast.makeText(getApplicationContext(),"Please enter your E-mail address",Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(pass)){
Toast.makeText(getApplicationContext(),"Please enter your Password",Toast.LENGTH_LONG).show();
}
if (pass.length() == 0){
Toast.makeText(getApplicationContext(),"Please enter your Password",Toast.LENGTH_LONG).show();
}
if (pass.length()<8){
Toast.makeText(getApplicationContext(),"Password must be more than 8 digit",Toast.LENGTH_LONG).show();
}
else{
auth.createUserWithEmailAndPassword(email,pass)
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Userinformation userinformation = new Userinformation(
name,
kredi
);
databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(userinformation).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
startActivity(new Intent(SignUpActivity.this, HomeActivity.class));
finish();
}
else{
Toast.makeText(SignUpActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
else {
Toast.makeText(SignUpActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
}
public void navigate_sign_in(View v){
Intent inent = new Intent(this, SignInActivity.class);
startActivity(inent);
}
}
和用戶信息 class:
public class Userinformation {
public String name;
public String kredi;
public Userinformation(){
}
public Userinformation(String name,String kredi){
this.name = name;
this.kredi = kredi;
}
public String getUserName() {
return name;
}
public String getUserKredi() {
return kredi;
}
}
我只想保存這些屬性一次。 我怎樣才能做到這一點?
問題出在你的pojo上,改變這個
public String getUserName() {
return name;
}
public String getUserKredi() {
return kredi;
}
對此
public String getName() {
return name;
}
public String getKredi() {
return kredi;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.