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

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

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

问题

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

wohR9lJXnMgj9zCYMaasZDctrhf1
kredi: 
"20"
name: 
"test"
userKredi: 
"20"
userName: 
"test"

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

// 导入省略...

public class SignUpActivity extends AppCompatActivity {

    // 控件和成员变量省略...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 省略部分...
    }
    
    private void signUp() {
        // 省略部分...
        final String kredi = "20";
        final String name = kullaniciAdi.getText().toString().trim();
        // 省略部分...
    }

    // 其他部分省略...
}

以及Userinformation类:

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;
    }
}

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

英文:

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:

wohR9lJXnMgj9zCYMaasZDctrhf1
kredi: 
"20"
name: 
"test"
userKredi: 
"20"
userName: 
"test"

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

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);
    }
}

and Userinformation 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;
    }
}

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

答案1

得分: 1

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

public String getName() {
    return name;
}
public String getKredi() {
    return kredi;
}
英文:

The problem is at your pojo, change this

public String getUserName() {
        return name;
    }
    public String getUserKredi() {
        return kredi;
    }

to this

public String getName() {
        return name;
    }
    public String getKredi() {
        return kredi;
    }

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:

确定