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 [email protected] --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 = "[email protected]";
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 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 Authent
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 Use Performance Console in Firebase with Android?
You can use the Firebase Performance Monitoring service to learn more about the performance traits of your Apple, Android, and web apps. As you roll out new features or make configuration changes, Firebase Performance Monitoring, a real-time app performance monitoring tool, enables you to keep a clo
6 min read
How do we take password input in HTML forms ?
In this article, we learn how to create a password input field in HTML. This can be done by using the <input> tag with type attribute which is set to the value "password". It involves sensitive information about the data of the user. The text in the input password field will be changed into bu
1 min read
How to Use Firebase Cloud Messaging (FCM) in Android?
Firebase Cloud Messaging (FCM) is a service provided by Google that allows developers to send push notifications to Android, iOS, and web devices. FCM uses Google Cloud Messaging (GCM) as the underlying technology for sending and receiving messages but offers additional features such as the ability
3 min read
How to Use Remote Config Console in Firebase?
With the help of the cloud service Firebase Remote Config, you may modify the behavior and aesthetics of your app without having to wait for users to download an update. Create in-app default values that regulate your app's behavior and appearance while utilizing Remote Config. You can later overrid
5 min read
How to Use Hosting Console in Firebase?
For developers, Firebase Hosting offers production-quality online content hosting. You may easily launch web applications and send both static and dynamic content to a global CDN with a single command (content delivery network). You can host your app's static assets (HTML, CSS, JavaScript, media fil
3 min read