javaandroidfirebase-authentication

Display Password from FireBase Authentication on Profile


I want to show the user's password in the ProfileFragment, but I don't know how. The user's password is stored in FirebaseAuthentication. I was able to show the Username and Email, but not the password. Do I have to create a Real Time Database and add existing data, including the password, to be able to show it?

Login.java:

public class FormLogin extends AppCompatActivity {
    EditText loginEmail;
    EditText loginPassword;
    TextView signupText, forgetTextLink;
    Button loginButton;
    FirebaseAuth fAuth;

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser user = fAuth.getCurrentUser();

        if(user != null) {

            startActivity(new Intent(FormLogin.this, Drawer.class));
            finish();

        }
    }

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

        loginEmail = findViewById(R.id.login_email);
        loginPassword = findViewById(R.id.login_password);
        loginButton = findViewById(R.id.login_button);
        signupText = findViewById(R.id.signupText);
        forgetTextLink = findViewById(R.id.forgetText);



        fAuth = FirebaseAuth.getInstance();

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String Email = loginEmail.getText().toString();
                String Senha = loginPassword.getText().toString();

            if (!Email.isEmpty() && Patterns.EMAIL_ADDRESS.matcher(Email).matches()){
                if (!Senha.isEmpty()){
                    fAuth.signInWithEmailAndPassword(Email, Senha)
                            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                                @Override
                                public void onSuccess(AuthResult authResult) {
                                    Toast.makeText(FormLogin.this, "Login Efetuado!" , Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(FormLogin.this, Drawer.class));
                                    finish();
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(FormLogin.this, "Login Falhou!" , Toast.LENGTH_SHORT).show();
                                }
                            });
                    }
                else{
                    loginPassword.setError("Preencha todos os campos!");
                }
                }else if (Email.isEmpty()){
                    loginEmail.setError("Preencha todos os campos!");
                }else {
                    loginEmail.setError("Informe um Email válido");
                }
            }
            });

            forgetTextLink.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final EditText resetMail = new EditText(v.getContext());
                    final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext());
                    passwordResetDialog.setTitle("Redefinir Senha?");
                    passwordResetDialog.setMessage("Para redefinir sua senha, informe o e-mail" +
                            " cadastrado na sua conta e lhe enviaremos um link com as instruções.");
                    passwordResetDialog.setView(resetMail);

                    passwordResetDialog.setPositiveButton("Enviar", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // extract the email and send reset link
                            String mail = resetMail.getText().toString();
                            fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Toast.makeText(FormLogin.this, "Link de redefinição enviado para o e-mail informado.", Toast.LENGTH_SHORT).show();
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(FormLogin.this, "Erro! Link de redefinição não enviado", Toast.LENGTH_SHORT).show();
                                }
                            });

                        }
                    });

                    passwordResetDialog.setNegativeButton("Fechar", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // close the dialog
                        }
                    });

                    passwordResetDialog.create().show();

                }
            });
        signupText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FormLogin.this, FormCadastro.class);
                startActivity(intent);
        };
    });
}
}

Profile.java

public class ProfileFragment extends Fragment {

    TextView profileUsername, profileEmail, profilePassword;
    TextView titleUsername, titleEmail;
    ImageView profileImg;
    FirebaseAuth fAuth;
    FirebaseUser currentUser;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        fAuth = FirebaseAuth.getInstance();
        currentUser = fAuth.getCurrentUser();

        profileUsername = view.findViewById(R.id.profileUser);
        profileEmail = view.findViewById(R.id.profileEmail);
        profilePassword = view.findViewById(R.id.profilePassword);
        profileImg = view.findViewById(R.id.profileImg);

        titleEmail = view.findViewById(R.id.titleEmail);
        titleUsername = view.findViewById(R.id.titleUsername);

        updateProfileFragment();
        return view;

    }

    private void updateProfileFragment() {
        profileEmail.setText(currentUser.getEmail());
        titleEmail.setText(currentUser.getEmail());
        profileUsername.setText(currentUser.getDisplayName());
        titleUsername.setText(currentUser.getDisplayName());

        Glide.with(this).load(currentUser.getPhotoUrl()).into(profileImg);
    }
}

Solution

  • There is no way to retrieve the user's password from Firebase Authentication. In fact, having such a feature is an incredible security risk - so I recommend also not implementing your own password storage as a workaround.

    The only moment you should allow the user to see what password they entered, is when they just entered it - as it helps them catch typos. Not surprisingly that is what the major sign-in providers, such as Google and Facebook, do - but none of them have the ability to display a previously entered password value, a behavior I recommend mimicking in your own app too.