Firebase

Introduction

In part one of this series, we learned about features of Firebase authentication, how to start/enable email authentication and how to sign-in and sign-up with email and password in Firebase. New to email and password authentication? First, read part one of this article. In this part, we will learn the following features:

FORGOT PASSWORD

Firebase provides the sendPasswordResetEmail() method to reset password. It automatically sends the Reset Password email to your entered email. To reset a password, click the link in your email.
Note
We can customize the Email Template in Email Templates Tab in Authentication Option.
  1. // Method to Reset Password or Forget Password Option
  2. auth.sendPasswordResetEmail(modeStr).addOnCompleteListener(new OnCompleteListener() {
  3. @Override
  4. public void onComplete(@NonNull Task task) {
  5. if (task.isSuccessful()) {
  6. Toast.makeText(ForgetAndChangePasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
  7. } else {
  8. Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
  9. }
  10. PD.dismiss();
  11. }
  12. });
CHANGE USERNAME OR PASSWORD
Firebase provides updatePassword() to change the password and updateEmail() to change your Email ID.
  1. // Method to change Password Option
  2. user.updatePassword(modeStr)
  3. .addOnCompleteListener(new OnCompleteListener() {
  4. @Override
  5. public void onComplete(@NonNull Task task) {
  6. if (task.isSuccessful()) {
  7. Toast.makeText(ForgetAndChangePasswordActivity.this, "Password is updated!", Toast.LENGTH_SHORT).show();
  8. } else {
  9. Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to update password!", Toast.LENGTH_SHORT).show();
  10. }
  11. PD.dismiss();
  12. }
  13. });
  14. // Method to Change Email or Username Option
  15. user.updateEmail(modeStr)
  16. .addOnCompleteListener(new OnCompleteListener() {
  17. @Override
  18. public void onComplete(@NonNull Task task) {
  19. if (task.isSuccessful()) {
  20. Toast.makeText(ForgetAndChangePasswordActivity.this, "Email address is updated.", Toast.LENGTH_LONG).show();
  21. } else {
  22. Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to update email!", Toast.LENGTH_LONG).show();
  23. }
  24. PD.dismiss();
  25. }
  26. });
DELETE USER ACCOUNT
Firebase provides delete() method to delete an account.
  1. // Method to Remove Account Option
  2. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  3. if (user != null) {
  4. user.delete()
  5. .addOnCompleteListener(new OnCompleteListener() {
  6. @Override
  7. public void onComplete(@NonNull Task task) {
  8. if (task.isSuccessful()) {
  9. Toast.makeText(ForgetAndChangePasswordActivity.this, "Your profile is deleted:", Toast.LENGTH_SHORT).show();
  10. } else {
  11. Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to delete your account!", Toast.LENGTH_SHORT).show();
  12. }
  13. PD.dismiss();
  14. }
  15. });
  16. }
CHECKING USER SESSION
We can check if the user is logged in or not with Firebase using the following method.
  1. //Checking user is present or not
  2. FirebaseAuth auth = FirebaseAuth.getInstance();
  3. if (auth.getCurrentUser() != null) {
  4. Log.v("Session", "Signed In");
  5. }
SIGN OUT USER SESSION
Firebase provides signOut() method to Sign out/Clear Firebase User Session.
  1. // Sign-Out option
  2. btnSignOut.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View view) {
  5. auth.signOut();
  6. // this listener will be called when there is change in firebase user session
  7. FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
  8. @Override
  9. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  10. FirebaseUser user = firebaseAuth.getCurrentUser();
  11. if (user == null) {
  12. startActivity(new Intent(MainActivity.this, LoginActivity.class));
  13. finish();
  14. }
  15. }
  16. };
  17. }
  18. });
From Part 1 & 2 of the article, we have learned how to do Firebase Email & Password Authentication with its awesome features.
Download Code
In this, I explained each method to handle the Firebase Email/Password method and am not explaining the layout of each screen in this project. For more details, please download the whole project source from GitHub.
If you feel this article is informative, do like and star the repo on GitHub. You can download the full sample code here.