2
votes

I have a database from firebase. I want to retrieve some data using a user's email. suppose user put his/her email if the email exists in the firebase database then it shows his/her username and password. I am not using firebase authentication, I am using firebase realtime database. here is my database structure: 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());
        }
    });
}

here is my model class: public class 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;
}

}

How can I do this?

1

1 Answers

0
votes

Using firebase database to create your own authentication system is not a good idea. What you want to do will require you to allow read of every user's email password stored in database. This will be a huge security risk for your app as anyone can read all the email passwords in your database.

Firebase provides easy to use firebase authentication with many options to use. You can easily delegate the security to firebase auth and store your user's profile and data in db. This way you can also restrict data access for each user (which is why you want email password login for your app).

Please see the authentication documents of firebase: https://firebase.google.com/docs/auth

It will help you create your app without implementing your own auth and focus on actual features development.