1
votes

I am developing an Android Application with Firebase User Authentication. The problem I am facing is that I get email and password from user and then create that user into firebase. I am not verifying email which user has entered. Now I want to implement reset password feature. For that Firebase provides resetPassword method and send reset password email to that particular user. But the question is that if email is not exist then what should we do?

Here is the code I am using to register user in Firebase:

private void registerUser(){

        //creating a new user
        firebaseAuth.createUserWithEmailAndPassword("user email here", "user password here")
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        //checking if success
                        if(task.isSuccessful()){
                            //display some message here 
                        }else{
                            //display some message here 
                        }

                    }
                });

    }

Please let me know if there is any alternate option available for this feature. Thanks.

2

2 Answers

3
votes

An alternative would be to use the Firebase Admin SDK to change the user's password. From the documentation on updating user information:

The updateUser() method allows you to modify an existing user's data. It accepts a uid for the user to update as well as an object containing the UserRecord properties to update:

admin.auth().updateUser(uid, {
  email: "[email protected]",
  emailVerified: true,
  password: "newPassword",
  displayName: "Jane Doe",
  photoURL: "http://www.example.com/12345678/photo.png",
  disabled: true
})
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully updated user", userRecord.toJSON());
  })
  .catch(function(error) {
    console.log("Error updating user:", error);
  });

With:

password- string - The user's new raw, unhashed password. Must be at least six characters long.

This part of the Firebase Admin SDK is currently only available in Node.js. But if you don't have a Node.js server yet, you could implement the functionality in Cloud Functions for Firebase.

1
votes

Please try with below code may be help you , I am using this.

private FirebaseUser user;
user = FirebaseAuth.getInstance().getCurrentUser();
final String email = user.getEmail();
AuthCredential credential = EmailAuthProvider.getCredential(email,oldpass);
user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
 public void onComplete(@NonNull Task<Void> task) {
    if(task.isSuccessful()){
         user.updatePassword(newPass).addOnCompleteListener(new OnCompleteListener<Void>() {
             @Override
             public void onComplete(@NonNull Task<Void> task) {
                 if(!task.isSuccessful()){
                     Snackbar snackbar_fail = Snackbar
                                       .make(coordinatorLayout, "Something went wrong. Please try again later", Snackbar.LENGTH_LONG);
                                         snackbar_fail.show();
                   }else {
                      Snackbar snackbar_su = Snackbar
                                          .make(coordinatorLayout, "Password Successfully Modified", Snackbar.LENGTH_LONG);
                                            snackbar_su.show();
                                        }
                                    }
                                });
                   }else {
                            Snackbar snackbar_su = Snackbar
                                    .make(coordinatorLayout, "Authentication Failed", Snackbar.LENGTH_LONG);
                            snackbar_su.show();
                        }
                    }
                });
            }
}