仅当密码正确时,Firebase才会更改“DisplayName”

时间:2017-12-31 21:43:29

标签: java android firebase firebase-authentication

我正在处理我的firebase应用程序,我需要一些逻辑帮助,因为目前它没有按预期工作。

我试图这样做,以便登录用户只有在输入正确的密码时才能更改其名称。

使用我当前的代码,它会更改名称,尽管输入的密码不正确的Toast消息。

/**
     * Update Name Only
     */
    private void updateDisplayNameOnly(){

        showProgress();

        AuthCredential credential = EmailAuthProvider
                .getCredential(FirebaseAuth.getInstance().getCurrentUser().getEmail(), mConfirm.getText().toString());

        FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential)
                .addOnCompleteListener(new OnCompleteListener<Void>()
                {
                    @Override
                    public void onComplete(@NonNull Task<Void> task)
                    {
                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

                        UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
                                .setDisplayName(mName.getText().toString())
                                //.setPhotoUri(Uri.parse("https://avatarfiles.alphacoders.com/862/86285.jpg"))
                                .build();

                        user.updateProfile(profileUpdate);

                        if (task.isSuccessful())
                        {
                            Log.d(TAG, "onComplete: User Profile updated");
                            hideProgress();

                            Toast.makeText(getActivity(), "NAME IS UPDATED XD XD XD", Toast.LENGTH_SHORT).show();

                            // RESTART APP FOR CHANGES
                        }

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        hideProgress();
                        Toast.makeText(getActivity(), "NAME WAS NOT UPDATED 2", Toast.LENGTH_SHORT).show();
                    }
                });

    }

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

您的问题可能是在您的方法中将Task<Void>设置为Task<AuthResult>。尝试将代码修改为以下内容:

    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if(task.isSuccessful())
        {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

            UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
                    .setDisplayName(mName.getText().toString())
                    //.setPhotoUri(Uri.parse("https://avatarfiles.alphacoders.com/862/86285.jpg"))
                    .build();

            user.updateProfile(profileUpdate);

            Log.d(TAG, "onComplete: User Profile updated");
            Toast.makeText(getActivity(), "NAME IS UPDATED XD XD XD", Toast.LENGTH_SHORT).show();

        }
        } else {
            //Display what happens if you couldn't update the name
        }
        hideProgressDialog();
   });