Change Firebase Password | Android Studio | Kotlin
How to change the password of an account on Firebase?
In this tutorial, we will programmatically update/change the password of an existing email/password account on Firebase using Android Studio IDE and Kotlin language.
First of all, we will reauthenticate the user by authenticating the old/previous password, if that password is correct then input a new password to update.
>>Watch For Java
Required Libraries:
dependencies { . . . //firebase libs implementation 'com.google.firebase:firebase-auth:19.2.0' implementation 'com.google.firebase:firebase-core:17.2.3' . . . }
ctivity_main.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="#FAF9F9" android:gravity="center" android:padding="10dp" tools:context=".MainActivity"> <TextView android:id="@+id/labelTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Update Password" android:textAlignment="center" android:textColor="#000" android:textSize="25sp" android:textStyle="bold" /> <EditText android:id="@+id/oldPassEt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/labelTv" android:layout_marginTop="40dp" android:background="#fff" android:hint="Old Password" android:inputType="textPassword" android:padding="10dp" /> <EditText android:id="@+id/newPassEt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/oldPassEt" android:layout_marginTop="10dp" android:background="#fff" android:hint="New Password" android:inputType="textPassword" android:padding="10dp" /> <Button android:id="@+id/updatePasswordBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/newPassEt" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="Update Password" /> </RelativeLayout>
MainActivity.kt
package com.blogspot.atifsoftwares.myapplication import android.app.ProgressDialog import android.os.Bundle import android.text.TextUtils import android.view.View import android.widget.Button import android.widget.EditText import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.firebase.auth.EmailAuthProvider import com.google.firebase.auth.FirebaseAuth import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { private lateinit var firebaseAuth: FirebaseAuth private lateinit var pd: ProgressDialog override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //init FirebaseAuth for use authentication firebaseAuth = FirebaseAuth.getInstance() //setup progress dialog pd = ProgressDialog(this) pd.setMessage("Please Wait...") //handle click event to begin updatePasswordBtn.setOnClickListener { //input data val oldPassword = oldPassEt.text.toString().trim() val newPassword = newPassEt.text.toString().trim() //validate data if (TextUtils.isEmpty(oldPassword)) { Toast.makeText(applicationContext, "Enter your current password...", Toast.LENGTH_SHORT).show() } else if (newPassword.length < 6) { Toast.makeText(applicationContext, "Password length must atleast 6 characters...", Toast.LENGTH_SHORT).show() } else { updatePassword(oldPassword, newPassword) } } } private fun updatePassword(oldPassword: String, newPassword: String) { //show dialog pd.show() //get current user val user = firebaseAuth.currentUser //before changing password re-authenticate the user val authCredential = EmailAuthProvider.getCredential(user!!.email!!, oldPassword) user.reauthenticate(authCredential) .addOnSuccessListener { //successfully authenticated, begin update user.updatePassword(newPassword) .addOnSuccessListener { //password updated pd.dismiss() Toast.makeText(applicationContext, "Password Updated...", Toast.LENGTH_SHORT).show() } .addOnFailureListener { e -> //failed updating password, show reason pd.dismiss() Toast.makeText(applicationContext, "" + e.message, Toast.LENGTH_SHORT).show() } } .addOnFailureListener { e -> //authentication failed, show reason pd.dismiss() Toast.makeText(applicationContext, "" + e.message, Toast.LENGTH_SHORT).show() } } }
Output
Comments
Post a Comment