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
- Change Username/Email
- Change Password
- Delete Account
- Checking User Session
- Clear/Sign-out the Session
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.
-
- auth.sendPasswordResetEmail(modeStr).addOnCompleteListener(new OnCompleteListener() {
- @Override
- public void onComplete(@NonNull Task task) {
- if (task.isSuccessful()) {
- Toast.makeText(ForgetAndChangePasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
- }
- PD.dismiss();
- }
- });
CHANGE USERNAME OR PASSWORD
Firebase provides updatePassword() to change the password and updateEmail() to change your Email ID.
-
- user.updatePassword(modeStr)
- .addOnCompleteListener(new OnCompleteListener() {
- @Override
- public void onComplete(@NonNull Task task) {
- if (task.isSuccessful()) {
- Toast.makeText(ForgetAndChangePasswordActivity.this, "Password is updated!", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to update password!", Toast.LENGTH_SHORT).show();
- }
- PD.dismiss();
- }
- });
-
-
- user.updateEmail(modeStr)
- .addOnCompleteListener(new OnCompleteListener() {
- @Override
- public void onComplete(@NonNull Task task) {
- if (task.isSuccessful()) {
- Toast.makeText(ForgetAndChangePasswordActivity.this, "Email address is updated.", Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to update email!", Toast.LENGTH_LONG).show();
- }
- PD.dismiss();
- }
- });
DELETE USER ACCOUNT
Firebase provides delete() method to delete an account.
-
- FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
- if (user != null) {
- user.delete()
- .addOnCompleteListener(new OnCompleteListener() {
- @Override
- public void onComplete(@NonNull Task task) {
- if (task.isSuccessful()) {
- Toast.makeText(ForgetAndChangePasswordActivity.this, "Your profile is deleted:", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(ForgetAndChangePasswordActivity.this, "Failed to delete your account!", Toast.LENGTH_SHORT).show();
- }
- PD.dismiss();
- }
- });
- }
CHECKING USER SESSION
We can check if the user is logged in or not with Firebase using the following method.
-
- FirebaseAuth auth = FirebaseAuth.getInstance();
- if (auth.getCurrentUser() != null) {
- Log.v("Session", "Signed In");
- }
SIGN OUT USER SESSION
Firebase provides signOut() method to Sign out/Clear Firebase User Session.
-
- btnSignOut.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- auth.signOut();
-
- FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = firebaseAuth.getCurrentUser();
- if (user == null) {
- startActivity(new Intent(MainActivity.this, LoginActivity.class));
- finish();
- }
- }
- };
- }
- });
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.