如何使用电子邮件从Firebase实时数据库检索数据?

时间:2019-07-09 18:13:39

标签: java android firebase-realtime-database

我有一个来自Firebase的数据库。我想使用用户的电子邮件检索一些数据。假设用户将自己的电子邮件(如果该电子邮件存在于Firebase数据库中)则显示其用户名和密码。我未使用Firebase身份验证,但正在使用Firebase实时数据库。 这是我的数据库结构: database structure

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forgate_password);

    emailf = findViewById(R.id.emailf);
    userf = findViewById(R.id.usernamef);
    passwordf = findViewById(R.id.passwordf);

    ok = findViewById(R.id.okbtn);

    database = FirebaseDatabase.getInstance();
    users = database.getReference("Users").child("emailAddress");

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signInMethod(emailf.getText().toString());
        }
    });
}

private void signInMethod(final String email) {

    users.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            if (dataSnapshot.child("emailAddress").exists()){
                if (dataSnapshot.child(user.getUserName()).exists()){
                    Toast.makeText(ForgatePassword.this,"User already exists",Toast.LENGTH_LONG).show();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("Tag", "Email not exists", error.toException());
        }
    });
}

这是我的模型课: 公共类SignInUpModel {

private String fullName;
private String userName;
private String schoolName;
private String className;
private String division;
private String phnNumber;
private String emailAddress;
private String reference;
private String password;

public SignInUpModel() {

}

public SignInUpModel(String fullName, String userName, String schoolName, String className, String division, String phnNumber, String emailAddress, String reference, String password) {
    this.fullName = fullName;
    this.userName = userName;
    this.schoolName = schoolName;
    this.className = className;
    this.division = division;
    this.phnNumber = phnNumber;
    this.emailAddress = emailAddress;
    this.reference = reference;
    this.password = password;
}

public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getSchoolName() {
    return schoolName;
}

public void setSchoolName(String schoolName) {
    this.schoolName = schoolName;
}

public String getClassName() {
    return className;
}

public void setClassName(String className) {
    this.className = className;
}

public String getDivision() {
    return division;
}

public void setDivision(String division) {
    this.division = division;
}

public String getPhnNumber() {
    return phnNumber;
}

public void setPhnNumber(String phnNumber) {
    this.phnNumber = phnNumber;
}

public String getEmailAddress() {
    return emailAddress;
}

public void setEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;
}

public String getReference() {
    return reference;
}

public void setReference(String reference) {
    this.reference = reference;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用firebase数据库创建您自己的身份验证系统不是一个好主意。 您要执行的操作将要求您允许读取存储在数据库中的每个用户的电子邮件密码。由于任何人都可以读取数据库中的所有电子邮件密码,因此这会对您的应用造成巨大的安全风险。

Firebase提供了易于使用的Firebase身份验证,其中包含许多可用选项。您可以轻松地将安全性委派给firebase auth,并将用户的配置文件和数据存储在db中。这样,您还可以限制每个用户的数据访问权限(这就是为什么要为应用程序登录电子邮件密码的原因)。

请参阅Firebase的身份验证文档:https://firebase.google.com/docs/auth

它将帮助您创建应用程序而无需实施自己的身份验证,而是专注于实际功能的开发。