How to update password in flutter using Firestore
519
Use this function,
void _changePassword(String yourPassword) async{
FirebaseUser user = await FirebaseAuth.instance.currentUser();
user.updatePassword(yourPassword).then((_){
Firestore.instance.collection("users").document(user.uid).updateData(
{
"password" : _newpasswordController.text,
}).then((_){
print("Successfully changed password");
});
}).catchError((error){
print("Error " + error.toString());
});
}
on press,
onPressed: (){
if(!_formKey.currentState.validate()){
return;
}
_formKey.currentState.save();
_changePassword(_newpasswordController.text);
},
Author by
New user
Updated on December 30, 2022Comments
-
New user over 1 year
Hi guys I'm new in flutter and I'm trying to make an update password based on the user data that stored in the firestore and also based on the user current login to the system. Anyone know how to update password based on certain ID that login to the system and validate it when the user enter the repeat password? Please help.
Code
import 'package:flutter/material.dart'; import 'package:monger_app/localization/localization_constants.dart'; import 'package:monger_app/page/setting.dart'; import 'package:monger_app/theme/colors.dart'; class BackupSettings extends StatefulWidget { @override _BackupSettingsState createState() => _BackupSettingsState(); } class _BackupSettingsState extends State<BackupSettings> { final _newpasswordController = TextEditingController(); final _repeatpasswordController = TextEditingController(); var _formKey = GlobalKey<FormState>(); bool checkCurrentPasswordValid = true; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(getTranslated(context, 'backup_text'),), elevation: 0, brightness: Brightness.light, backgroundColor: primary, leading: IconButton( onPressed: (){ Navigator.pop(context, MaterialPageRoute(builder: (context) => Settings())); }, icon: Icon(Icons.arrow_back_ios, size: 20, color: Colors.black,), ), ), body: SingleChildScrollView( child: Padding( padding: EdgeInsets.all(20.0), child: Column( key: _formKey, children: <Widget>[ Container( child: TextFormField( validator: (input) { if (input.length < 8) return 'Please Provide Minimum 8 Character'; }, decoration: InputDecoration( labelText: getTranslated(context, 'new_password'), labelStyle: TextStyle(color: Colors.grey), prefixIcon: Icon( Icons.lock, color: secondary, ), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: secondary), ), ), obscureText: true, controller: _newpasswordController ), ), Container( child: TextFormField( decoration: InputDecoration( labelText: getTranslated(context, 'repeat_password'), labelStyle: TextStyle(color: Colors.grey), prefixIcon: Icon( Icons.lock, color: secondary, ), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: secondary), ), ), obscureText: true, controller: _repeatpasswordController, validator: (value) { return _newpasswordController.text == value ? null : "Please validate your entered password"; }, ), ), SizedBox(height: 20), RaisedButton( padding: EdgeInsets.fromLTRB(70, 10, 70, 10), onPressed: (){ }, child: Text(getTranslated(context, 'save_button'), style: TextStyle( color: Colors.white, fontSize: 20.0, fontWeight: FontWeight.bold)), color: primary, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20.0), ), ) ], ), ), ), ); } }
-
New user almost 3 yearshi, may I know how to that?
-
New user almost 3 yearshi may I know how to do that?
-
Dharmaraj almost 3 yearsAre you using Firebase Authentication and still storing the password in Firestore?
-
New user almost 3 yearsyes I'm using firebase authentication and storing to the firestore @Dharmaraj
-
-
New user almost 3 yearsand what should I input in on press button?
-
Rajni Gujarati almost 3 yearscheck edited answer. let me know if it worked or not.
-
New user almost 3 yearsI check the password in the firestore it did not change but in the run it shows D/FirebaseAuth(27924): Notifying id token listeners about user ( PyDw717LcSTofi7dB4d3kV9QhIY2 ). W/System (27924): Ignoring header X-Firebase-Locale because its value was null. I/flutter (27924): Successfully changed password.
-
New user almost 3 yearsbut in the run it show successfully changed password, but actually it did not change the password (in firestore)
-
Rajni Gujarati almost 3 yearsit will not change password in firestore database. it will change in authentication table. for change in db you have to update that particular user data. check updated answer.
-
New user almost 3 yearsoh yess!! thank you it works. but how can I validate the text fields if the text fields are empty and if the new password text fields is not the same with the repeat password text fields?
-
New user almost 3 yearsI got this error The method 'validate' was called on null. Receiver: null Tried calling: validate()