Firebase身份验证与电子邮件和密码额外字段重复 – Android – Java

huangapple go评论108阅读模式
英文:

Firebase authentication with email and password extra fields duplicating - Android - Java

问题

我正在尝试使用电子邮件和密码对用户进行身份验证,并且当有人要注册时,我想要添加两个额外的字段(姓名和kredi)。但是当我注册我的应用时,还有两个带有“user”标签的字段保存到Firebase实时数据库中,如下所示:

  1. wohR9lJXnMgj9zCYMaasZDctrhf1
  2. kredi:
  3. "20"
  4. name:
  5. "test"
  6. userKredi:
  7. "20"
  8. userName:
  9. "test"

我不明白为什么会发生这种情况。这是我的注册活动代码:

  1. import androidx.annotation.NonNull;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. // 导入省略...
  4. public class SignUpActivity extends AppCompatActivity {
  5. // 控件和成员变量省略...
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. // 省略部分...
  9. }
  10. private void signUp() {
  11. // 省略部分...
  12. final String kredi = "20";
  13. final String name = kullaniciAdi.getText().toString().trim();
  14. // 省略部分...
  15. }
  16. // 其他部分省略...
  17. }

以及Userinformation类:

  1. public class Userinformation {
  2. public String name;
  3. public String kredi;
  4. public Userinformation(){
  5. }
  6. public Userinformation(String name,String kredi){
  7. this.name = name;
  8. this.kredi = kredi;
  9. }
  10. public String getUserName() {
  11. return name;
  12. }
  13. public String getUserKredi() {
  14. return kredi;
  15. }
  16. }

我只想保存这些属性一次。我该如何做到这一点?

英文:

I'm trying to authanticate users with email and password and I want to add two extra fields (name and kredi) when someone going to sign-up. But when I sign-up to my app, two more fields with "user" tag saving to firebase realtime database like this:

  1. wohR9lJXnMgj9zCYMaasZDctrhf1
  2. kredi:
  3. "20"
  4. name:
  5. "test"
  6. userKredi:
  7. "20"
  8. userName:
  9. "test"

I don't understand why this is happening. This is my sign-up activity code:

  1. import androidx.annotation.NonNull;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10. import com.google.android.gms.tasks.OnCompleteListener;
  11. import com.google.android.gms.tasks.Task;
  12. import com.google.firebase.auth.AuthResult;
  13. import com.google.firebase.auth.FirebaseAuth;
  14. import com.google.firebase.database.DatabaseReference;
  15. import com.google.firebase.database.FirebaseDatabase;
  16. public class SignUpActivity extends AppCompatActivity {
  17. EditText signUpMail,signUpPass,kullaniciAdi;
  18. Button signUpButton;
  19. private FirebaseAuth auth;
  20. private DatabaseReference databaseReference;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_sign_up);
  25. auth=FirebaseAuth.getInstance();
  26. signUpMail = findViewById(R.id.signUpMail);
  27. signUpPass = findViewById(R.id.signUpPass);
  28. kullaniciAdi = findViewById(R.id.kullaniciAdi);
  29. auth=FirebaseAuth.getInstance();
  30. signUpButton = findViewById(R.id.signUpButton);
  31. signUpButton.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View view) {
  34. signUp();
  35. }
  36. });
  37. }
  38. private void signUp() {
  39. final String email = signUpMail.getText().toString().trim();
  40. final String pass = signUpPass.getText().toString().trim();
  41. final String kredi = "20";
  42. final String name = kullaniciAdi.getText().toString().trim();
  43. if(TextUtils.isEmpty(email)){
  44. Toast.makeText(getApplicationContext(),"Please enter your E-mail address",Toast.LENGTH_LONG).show();
  45. return;
  46. }
  47. if(TextUtils.isEmpty(pass)){
  48. Toast.makeText(getApplicationContext(),"Please enter your Password",Toast.LENGTH_LONG).show();
  49. }
  50. if (pass.length() == 0){
  51. Toast.makeText(getApplicationContext(),"Please enter your Password",Toast.LENGTH_LONG).show();
  52. }
  53. if (pass.length()<8){
  54. Toast.makeText(getApplicationContext(),"Password must be more than 8 digit",Toast.LENGTH_LONG).show();
  55. }
  56. else{
  57. auth.createUserWithEmailAndPassword(email,pass)
  58. .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
  59. @Override
  60. public void onComplete(@NonNull Task<AuthResult> task) {
  61. if (task.isSuccessful()) {
  62. Userinformation userinformation = new Userinformation(
  63. name,
  64. kredi
  65. );
  66. databaseReference = FirebaseDatabase.getInstance().getReference();
  67. databaseReference.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(userinformation).addOnCompleteListener(new OnCompleteListener<Void>() {
  68. @Override
  69. public void onComplete(@NonNull Task<Void> task) {
  70. if(task.isSuccessful()){
  71. startActivity(new Intent(SignUpActivity.this, HomeActivity.class));
  72. finish();
  73. }
  74. else{
  75. Toast.makeText(SignUpActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
  76. }
  77. }
  78. });
  79. }
  80. else {
  81. Toast.makeText(SignUpActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
  82. }
  83. }
  84. });
  85. }
  86. }
  87. public void navigate_sign_in(View v){
  88. Intent inent = new Intent(this, SignInActivity.class);
  89. startActivity(inent);
  90. }
  91. }

and Userinformation class:

  1. public class Userinformation {
  2. public String name;
  3. public String kredi;
  4. public Userinformation(){
  5. }
  6. public Userinformation(String name,String kredi){
  7. this.name = name;
  8. this.kredi = kredi;
  9. }
  10. public String getUserName() {
  11. return name;
  12. }
  13. public String getUserKredi() {
  14. return kredi;
  15. }
  16. }

I just want to save those properties just once. How can I do this?

答案1

得分: 1

问题出在你的POJO类上,请将代码更改为:

  1. public String getName() {
  2. return name;
  3. }
  4. public String getKredi() {
  5. return kredi;
  6. }
英文:

The problem is at your pojo, change this

  1. public String getUserName() {
  2. return name;
  3. }
  4. public String getUserKredi() {
  5. return kredi;
  6. }

to this

  1. public String getName() {
  2. return name;
  3. }
  4. public String getKredi() {
  5. return kredi;
  6. }

huangapple
  • 本文由 发表于 2020年5月30日 20:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/62102666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定