Reset Firebase Password | Android Studio | Kotlin

How to reset the firebase password?

If the user forgot the password then the user can easily reset the password by entering the registered email. The user will receive a link to reset the password.
The User can send a password reset email to a user with the sendPasswordResetEmail method. For example:
activity_forgot_password.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:padding="10dp"
    tools:context=".ForgotPasswordActivity">

    <TextView
        android:id="@+id/label1Tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Recover Password"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/label2Tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/label1Tv"
        android:layout_centerHorizontal="true"
        android:text="Enter your email to get instructions to reset password"
        android:textColor="@color/colorPrimary" />

    <EditText
        android:id="@+id/emailEt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:background="@drawable/shapre_rect02"
        android:drawableStart="@drawable/ic_mail_gray"
        android:drawablePadding="5dp"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:padding="10dp" />

    <Button
        android:id="@+id/recoverBtn"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/emailEt"
        android:layout_centerHorizontal="true"
        android:minWidth="120dp"
        android:text="Recover" />

</RelativeLayout>

ForgotPasswordActivity.kt
package com.blogspot.atifsoftwares.grocery

import android.app.ProgressDialog
import android.os.Bundle
import android.util.Patterns
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_forgot_password.*

class ForgotPasswordActivity : AppCompatActivity() {
    
    private var firebaseAuth: FirebaseAuth? = null
    private var progressDialog: ProgressDialog? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_forgot_password)
        
        //init firebase auth
        firebaseAuth = FirebaseAuth.getInstance()

        //setup progress dialog
        progressDialog = ProgressDialog(this)
        progressDialog!!.setTitle("Please wait")
        progressDialog!!.setCanceledOnTouchOutside(false)
        
        recoverBtn.setOnClickListener { recoverPassword() }
    }

    private fun recoverPassword() {
        val email = emailEt!!.text.toString().trim()
        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            Toast.makeText(this, "Invalid Email...", Toast.LENGTH_SHORT).show()
            return
        }
        progressDialog!!.setMessage("Sending instructions to reset password...")
        progressDialog!!.show()
        firebaseAuth!!.sendPasswordResetEmail(email)
                .addOnSuccessListener { //instructions sent
                    progressDialog!!.dismiss()
                    Toast.makeText(this@ForgotPasswordActivity, "Password reset instructions sent to your email...", Toast.LENGTH_SHORT).show()
                }
                .addOnFailureListener { e -> //failed sending instructions
                    progressDialog!!.dismiss()
                    Toast.makeText(this@ForgotPasswordActivity, "${e.message}", Toast.LENGTH_SHORT).show()
                }
    }
}

Output
Reset Firebase Password | Android Studio | Kotlin

Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

Add a Back Button to Action Bar Android Studio (Kotlin)