使用firebase在android中重置密码

时间:2017-04-07 03:26:46

标签: android firebase firebase-authentication

我需要使用firebase在Android应用中重置密码。我已成功完成登录和注册过程。但我现在需要重置密码。

2 个答案:

答案 0 :(得分:2)

发送密码重置电子邮件

您可以使用sendPasswordResetEmail方法向用户发送密码重置电子邮件。例如:

 FirebaseAuth auth = FirebaseAuth.getInstance();
 String emailAddress = "user@example.com";
 auth.sendPasswordResetEmail(emailAddress)
.addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d(TAG, "Email sent.");
        }
    }
});

答案 1 :(得分:0)

在下面的代码中,我从用户那里获得了电子邮件地址,并且按钮的点击事件请求是通过提供的电子邮件地址发送的。在任务未完成之前,会显示进度条。

public class ResetPasswordActivity extends AppCompatActivity {

EditText editTextEmail; //to get email address
ProgressBar progressBar;    //show progress bar
Button buttonForgotPassword;    //button for click
FirebaseAuth firebaseAuth;
String email;

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

    firebaseAuth = FirebaseAuth.getInstance();

    /*      click event of button
    * */
    buttonForgotPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            email = editTextEmail.getText().toString();

            /*  check if email address is blank
            * */
            if (TextUtils.isEmpty(email)) {
                editTextEmail.setError("Fill Email");       //if blank display error
            }

            progressBar.setVisibility(View.VISIBLE);   //set visibility

            /*
            *       send request for reset password
            * */
            firebaseAuth.sendPasswordResetEmail(email).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {

                    if (task.isSuccessful()) {
                        Toast.makeText(ResetPasswordActivity.this, "Successfully send you response", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(ResetPasswordActivity.this, "Failed to Send", Toast.LENGTH_SHORT).show();
                    }
                    progressBar.setVisibility(View.GONE);
                }
            });
        }
    });
}

/*      UDF
* */
private void bindView() {
    editTextEmail = (EditText) findViewById(R.id.activity_reset_password_edit_text);
    progressBar = (ProgressBar) findViewById(R.id.activity_reset_progressBar);
    buttonForgotPassword = (Button) findViewById(R.id.activity_reset_password_forgot_password_button);
}

}