-1

Hello i simply want to match 2 fields if they match i want to send 1 of them to firebase. this is for changing password. however im getting error and it goes right away to the else statement: "Something went wrong, password did not get changed". cant understand why.

right now if 2 password does not match im getting the message: both fields much match this part is right

when 2 matches i get something went wrong : thats the else statement for public void oncomplete for firebase. it simply skip if the task is sucsessful and i dont understand why.

public void changePassword(){
   String userPasswordNew = newPassword.getText().toString(); // get pw
    String userPassword2New = newPassword2.getText().toString();

    if (userPasswordNew.equals(userPassword2New)){

        if(userPasswordNew.equals("") || userPassword2New.equals("")){
            Toast.makeText(ProfilePasswordUpdateActivity.this,"Fields can not be empty", Toast.LENGTH_SHORT).show();
        }

        else {
            firebaseUser.updatePassword(userPasswordNew).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(ProfilePasswordUpdateActivity.this,"Password changed",Toast.LENGTH_SHORT).show();
                        finish();
                    }else{
                        Toast.makeText(ProfilePasswordUpdateActivity.this,"Something went wrong, password did not get changed",Toast.LENGTH_LONG).show();
                    }
                }
            });
        }

    }
    else{
        Toast.makeText(ProfilePasswordUpdateActivity.this,"Both fields much match in order to change password",Toast.LENGTH_LONG).show();
    }

}

the xml binding for the inputs

 newPassword = findViewById(R.id.EditText_new_password);
 newPassword2 = findViewById(R.id.EditText_new_password2);

error

2020-01-15 08:57:44.007 11071-11071/com.example.examapplikation E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.examapplikation, PID: 11071 java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseUser.reauthenticate(com.google.firebase.auth.AuthCredential)' on a null object reference at com.example.examapplikation.ProfilePasswordUpdateActivity.changePassword(ProfilePasswordUpdateActivity.java:66) at com.example.examapplikation.ProfilePasswordUpdateActivity$1.onClick(ProfilePasswordUpdateActivity.java:50) at android.view.View.performClick(View.java:6597) at android.view.View.performClickInternal(View.java:6574) at android.view.View.access$3100(View.java:778) at android.view.View$PerformClick.run(View.java:25885) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

karrar
  • 11
  • 7
  • 1
    My guess is that it's throwing a FirebaseAuthRecentLoginRequiredException, could you add the stacktrace you're getting or confirm that that's the problem? If that's the case I'll explain an easy fix. – ssoBAekiL Jan 15 '20 at 03:12
  • how can i add this ? stacktrace – karrar Jan 15 '20 at 03:15
  • 1
    actually it worked before i added the second if statement where i also check if fields are empty and not only if the 2 inputs were equal. but then it crash if something was empty but changed password sucsesfully when both fields were equal. however now it goes directly to the else statement in the changepassword function to firebase. – karrar Jan 15 '20 at 03:23
  • Sorry that's not enough of it, try editing the question and adding it fully, or just search if FirebaseAuthRecentLoginRequiredException appears in it. From what you're saying it sounds like that would be the problem but I prefer to be sure before writing an answer so check that please – ssoBAekiL Jan 15 '20 at 03:29
  • but how does the if statement i created look. its correct or maybe some logical error i made in it? – karrar Jan 15 '20 at 03:39
  • That's correct, if it wasn't the code inside the else wouldn't execute. Anyway if you want to be sure try deleting it and running the code again, you will see that that's not the problem – ssoBAekiL Jan 15 '20 at 03:44
  • In the else part instead of showing a toast do `throw task.getException()` and you'll see the cause of the call failing. – Frank van Puffelen Jan 15 '20 at 04:58
  • Please read [How-to-get-StackTrace](https://stackoverflow.com/questions/7841232/java-android-how-to-print-out-a-full-stack-trace). – Ashish Jan 15 '20 at 05:08

2 Answers2

0

Hope this will help you. as nilsi's SO answer says you need to reauthenticate the user to change the Password. I made changes in your Logic might this will help you. Just reauthenticate user before change password.

String userPasswordNew = newPassword.getText().toString();
String userPassword2New = newPassword2.getText().toString();

if(!userPasswordNew.equals("") && !userPassword2New.equals("")){
    if(userPasswordNew.equals(userPassword2New)){
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        AuthCredential credential = EmailAuthProvider
                .getCredential("user@example.com", "password1234");
        user.reauthenticate(credential)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            user.updatePassword(userPasswordNew).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                        Log.d(TAG, "Password updated");
                                    } else {
                                        Log.d(TAG, "Error password not updated")
                                    }
                                }
                            });
                        } else {
                            Log.d(TAG, "Error auth failed")
                        }
                    }
                });
    }else{
        Log.d("Password", "Does Not Match");
    }
}else{
    Log.d("Password", "Fill the Edittext");
}
Ashish
  • 6,791
  • 3
  • 26
  • 48
-1

Some security-sensitive actions—such as deleting an account, setting a primary email address, and changing a password—require that the user has recently signed in. If you perform one of these actions, and the user signed in too long ago, the action fails with an error. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticateWithCredential.

var user = firebase.auth().currentUser;
var credential;

// Prompt the user to re-provide their sign-in credentials

user.reauthenticateWithCredential(credential).then(function() {
  // User re-authenticated.
}).catch(function(error) {
  // An error happened.
});
  • Please provide in android format and please don't copy and paste the another answer instead just provide link of answer – Ashish Jan 15 '20 at 04:58