Tarik Puplic
Tarik Puplic

Reputation: 31

Firebase-realtime-database: Change Password Button

I am trying to write an activity that allows the user to change his password. But I am having a hard time getting it running... I do not know where the issues are. I am very new to this so it could be something absolutely stupid :D I would really appreciate your help. Thank you in advance!

I have tried to read the password from a table in my Firebase database with getValue(Password.class) and if the conditions in the if-clauses comply it should overwrite the old password with the new one with ref.setValue(newpass). Can this even work like this?!

public class ChangePwActivity extends AppCompatActivity {
    Button back, change;
    TextView oldpw,newpw,newpwrep;
    final FirebaseDatabase smartlockgj= FirebaseDatabase.getInstance();
    final DatabaseReference ref= smartlockgj.getReference("data/password");





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pw);
            back=  findViewById(R.id.btback);
            change= findViewById(R.id.btchangepw);
            oldpw= findViewById(R.id.oldpw);
            newpw= findViewById(R.id.newpw);
            newpwrep= findViewById(R.id.newpwrep);

            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(ChangePwActivity.this, MainActivity.class));
                }
            });

            change.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ref.addValueEventListener(new ValueEventListener(){
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Password pw = dataSnapshot.getValue(Password.class);
                            String oldpass = oldpw.getText().toString();
                            String newpass = newpw.getText().toString();
                            String newpassrep = newpwrep.getText().toString();
                            if (pw.getPassword() == oldpass) {
                                if (newpass == newpassrep) {
                                    ref.setValue(newpass);

                                    }
                                else{
                                    //PushupNote einfügen"New password does not match"
                                    }


                                }
                            else {
                                //PushupNote einfügen:"Old Password was incorrect"
                                }


                            }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });


                }


                    });

        }






    }

Password.java

public class Password {
 private String password;

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

 public Password(String password) {
  this.password = password;
 }

}

Upvotes: 2

Views: 102

Answers (1)

Anshul
Anshul

Reputation: 76

Whenever strings are compared , it is usually preferred to use TextUtils, in your case ,in my opinion use

                       @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Password pw = dataSnapshot.getValue(Password.class);
                            String oldpass = oldpw.getText().toString();
                            String newpass = newpw.getText().toString();
                            String newpassrep = newpwrep.getText().toString();
// here change these
                            if (TextUtils.equal(pw.getPassword(),oldpass)) {
                                if (TextUtils.equal(newpass,newpassrep)) {
                                    ref.setValue(newpass);

                                    }
                                else{
                                    //PushupNote einfügen"New password does not match"
                                    }


                                }
                            else {
                                //PushupNoteeinfügen:"Old Password was incorrect"
                                }


                            }

But in my opinion, i would also like you to recommend ,if possible in your project use the firebase auth, for authentication, and use the uuid from users into a document of realtime db, to do related logic.

Upvotes: 1

Related Questions