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);      
        },
Share:
519
New user
Author by

New user

Updated on December 30, 2022

Comments

  • New user
    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
      New user almost 3 years
      hi, may I know how to that?
    • New user
      New user almost 3 years
      hi may I know how to do that?
    • Dharmaraj
      Dharmaraj almost 3 years
      Are you using Firebase Authentication and still storing the password in Firestore?
    • New user
      New user almost 3 years
      yes I'm using firebase authentication and storing to the firestore @Dharmaraj
  • New user
    New user almost 3 years
    and what should I input in on press button?
  • Rajni Gujarati
    Rajni Gujarati almost 3 years
    check edited answer. let me know if it worked or not.
  • New user
    New user almost 3 years
    I 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
    New user almost 3 years
    but in the run it show successfully changed password, but actually it did not change the password (in firestore)
  • Rajni Gujarati
    Rajni Gujarati almost 3 years
    it 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
    New user almost 3 years
    oh 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
    New user almost 3 years
    I got this error The method 'validate' was called on null. Receiver: null Tried calling: validate()