How to Update User Password in Firebase and Web ?
Last Updated :
31 Jul, 2024
In the previous article, we have done How to get a currently singed-in user in firebase, In this Article we will see how to update the user password. Firebase auth provides us a method called updatePassword that takes a string parameter as a new password. We can do it simply by passing the new password as an argument to the method updatePassword.
Note: The user must have recently signed in.
The sample output below gives an idea of what we are going to implement:
Syntax:
You can set a user’s password with the updatePassword method. By simply pass the new password to the method updatePassword.
const user = firebase.auth().currentUser;
const newPassword = getASecureRandomPassword();
user.updatePassword(newPassword).then(() => {
// Update successful.
}).catch((error) => {
// An error occurred
// ...
});
Note: To set the user’s password, the user must have signed in recently.
Step-by-Step Implementation:
Prerequisite: Before going to start must Add the firebase to your Web application. You may refer to this article.
Project structure: The project structure will look like this.

Step 2: After creating the Web application, Install the firebase module using the following command.
npm install firebase@8.3.1 --save
Step 3: Go to your firebase console and create a new project and copy your credentials.
const firebaseConfig = {
apiKey: "your api key",
authDomain: "your credentials",
projectId: "your credentials",
storageBucket: "your credentials",
messagingSenderId: "your credentials",
appId: "your credentials"
};index.html: We have only three buttons in the body of the HTML file that calls the method signin, showProfile, and UpdatePassword respectively. The definition of the above methods is defined in the JavaScript file.
script.js: JavaScript file that adds the firebase to the web app and methods that are used to sign in the user, Show the profile of the user, and Update the password method that takes the new password as a string and sets it to user uid. Sign-in method performs the user sign-in. If the user has an account. showUserProfile method shows the user profile data in the console. UpdatePassword actually performs the update of the password.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<script src=
"https://www.gstatic.com/firebasejs/3.7.4/firebase.js">
</script>
</head>
<body>
<script src="/app.js"></script>
<button onclick="signIn()">signIn</button>
<button onclick="showUserProfile1()">
Show user Profile data using provider
</button>
<div>
<h3>Enter New Password -</h3>
<input type="password"
name="New Password" id="newPassword">
<button onclick="UpdatePassword()">
Update Password
</button>
</div>
</body>
</html>
JavaScript
const firebaseConfig = {
// Firebase Configuration file
};
// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
var email = "...@gmail.com";
var password = "...";
function signIn() {
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then((userCredential) => {
// Signed in
var user = userCredential.user;
console.log("Sign In SuccessFul!");
// ...
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
});
}
function showUserProfile1() {
const user = firebase.auth().currentUser;
if (user !== null) {
user.providerData.forEach((profile) => {
console.log("Sign-in provider: " + profile.providerId);
console.log(" Provider-specific UID: " + profile.uid);
console.log(" Name: " + profile.displayName);
console.log(" Email: " + profile.email);
console.log(" Photo URL: " + profile.photoURL);
});
}
}
// Method to update the password
function UpdatePassword() {
const user = firebase.auth().currentUser;
const newPassword = document.getElementById('newPassword').value;
user.updatePassword(newPassword).then(() => {
// Update successful.
console.log('Update SuccessFul');
}).catch((error) => {
// An error occurred
// ...
});
}
Output:
Similar Reads
How to Change Password of User in Android using Firebase?
In many apps, we got a feature to login using our email and password. Sometimes it happens that we forget the password and most of the time there reset our password. Here we are going to implement the same feature to Reset our password using Firebase Authentication. You may refer to the following ar
3 min read
How to Send a User Verification Mail in Web and Firebase ?
In this article, we will see how to send the user verification email to the user, so that only verified users can be signed In to their account using Email Password Authentication. The Drawback of email password authentication is that, when a user signs up using an arbitrary email, firebase allows t
2 min read
How to get profile of signed in user in web and firebase ?
If you are creating a web app and use the firebase services in your web app project, Then you may also need to show the user profile on the profile page. If you successfully Authenticated the user using any method such as Email Password, Google, GitHub, Microsoft, etc. Then we can show the Authentic
3 min read
How to Get Currently Signed In User in Web and Firebase ?
Firebase is the most widely used Database and it has awesome features such as Authentication firestore, and Real-time database, given by firebase, We can integrate firebase into Web, Flutter, and Unity. But In this article, we are going to implement firebase on the web and get the current Signed In
3 min read
How to Use Storage Console in Firebase?
You may upload and share user-generated content, such as photographs and videos, using Cloud Storage for Firebase, which enables you to integrate rich media content into your apps. We will now go over how to set up and customize an Android application using Firebase so that it can leverage cloud sto
2 min read
How to match username and password in database in SQL ?
In this article, we are going to check the credentials (username and password) entered by the user. If credentials are matched from the database entries, the user can enter into another page and if credentials are incorrect then it displays "Sorry Invalid Username and Password". Pre-requisites: XAMP
4 min read
How to secure database passwords in PHP?
Most of the websites are providing sing up and login facility to the user. User has to create a password and use it for login to the website. But it is very important to secure the password of the user. password_hash() function provides the facility to securely store the password of the user to the
2 min read
How to upload files in firebase storage using ReactJS ?
Firebase Storage is a powerful cloud storage solution provided by Google's Firebase platform. It allows developers to store and retrieve user-generated content, such as images, videos, and other files, in a secure and scalable manner. In this article, we will explore how to upload files to Firebase
2 min read
How to show and hide Password in ReactJS?
To show and hide passwords in React JS we can simply use the password input with an input to select the show and hide state. The user might need to see what has he used as the password to verify which is an important concept in login and registration forms. We can create this type of password input
4 min read
How to change password of superuser in Django?
Django provides us Admin Panel for itâs users to look over the database and other activities. If you dont know how to create superuser then you can refer to How to create superuser in Django? How to change password of superuser in Django? For changing password of superuser, first reach the same dire
1 min read