I want to change current user password using Firebase in Flutter. Can any one help me on how to implement change password method?
I want to change current user password using Firebase in Flutter. Can any one help me on how to implement change password method?
I know this is a late post, but now it's possible to change the password of the logged-in user. Make sure to notify the user to log in again since this is a sensitive operation.
void _changePassword(String password) async{
//Create an instance of the current user.
FirebaseUser user = await FirebaseAuth.instance.currentUser();
//Pass in the password to updatePassword.
user.updatePassword(password).then((_){
print("Successfully changed password");
}).catchError((error){
print("Password can't be changed" + error.toString());
//This might happen, when the wrong password is in, the user isn't found, or if the user hasn't logged in recently.
});
}
If you are here for the solution in 2021 and getting reauthenticate error ->
void _changePassword(String currentPassword, String newPassword) async {
final user = await FirebaseAuth.instance.currentUser;
final cred = EmailAuthProvider.credential(
email: user.email, password: currentPassword);
user.reauthenticateWithCredential(cred).then((value) {
user.updatePassword(newPassword).then((_) {
//Success, do something
}).catchError((error) {
//Error, show something
});
}).catchError((err) {
});}
This should work according to the latest version of firebase:
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
User currentUser = firebaseAuth.currentUser;
currentUser.updatePassword("newpassword").then((){
// Password has been updated.
}).catchError((err){
// An error has occured.
})
This is currently not supported.
When this pull request is merged https://github.com/flutter/plugins/pull/678 the Flutter firebase_auth package will support that.
As @Gunter mentioned that the feature is currently still unavailable, you can make use of the firebase REST API way of changing the password for the time being.
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
Future<Null> changePassword(String newPassword) async {
const String API_KEY = 'YOUR_API_KEY';
final String changePasswordUrl =
'https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=$API_KEY';
final String idToken = await user.getIdToken(); // where user is FirebaseUser user
final Map<String, dynamic> payload = {
'email': idToken,
'password': newPassword,
'returnSecureToken': true
};
await http.post(changePasswordUrl,
body: json.encode(payload),
headers: {'Content-Type': 'application/json'},
)
}
You can get the idToken by using the getIdToken() method on the FirebaseUser object
You can get the firebase api key under the project setting in your console
Here's the latest method to change the password in firebase futter 2022
Future<bool> _changePassword(String currentPassword, String newPassword) async {
bool success = false;
//Create an instance of the current user.
var user = await FirebaseAuth.instance.currentUser!;
//Must re-authenticate user before updating the password. Otherwise it may fail or user get signed out.
final cred = await EmailAuthProvider.credential(email: user.email!, password: currentPassword);
await user.reauthenticateWithCredential(cred).then((value) async {
await user.updatePassword(newPassword).then((_) {
success = true;
usersRef.doc(uid).update({"password": newPassword});
}).catchError((error) {
print(error);
});
}).catchError((err) {
print(err);
});
return success;
}
Building on @Deepanshu Chowdhary's answer with a bit more error handling and some null safety (in my use case I can assume that both the user and email aren't null when I run this function, but you may need to check that first).
The function will return null if everything worked successfully, otherwise it will return a specific string for the error code where specified or just "Unknown".
static Future<String?> changePassword(String oldPassword, String newPassword) async {
User user = FirebaseAuth.instance.currentUser!;
AuthCredential credential = EmailAuthProvider.credential(email: user.email!, password: oldPassword);
Map<String, String?> codeResponses = {
// Re-auth responses
"user-mismatch": null,
"user-not-found": null,
"invalid-credential": null,
"invalid-email": null,
"wrong-password": null,
"invalid-verification-code": null,
"invalid-verification-id": null,
// Update password error codes
"weak-password": null,
"requires-recent-login": null
};
try {
await user.reauthenticateWithCredential(credential);
await user.updatePassword(newPassword);
return null;
} on FirebaseAuthException catch (error) {
return codeResponses[error.code] ?? "Unknown";
}
}