Search code examples
flutterdartgoogle-cloud-firestorefirebase-security

Resetting the user password only on first time sign in flutter


I would like to force a user password reset in Flutter on the first time login only. I have a field called "ChangePassword" in my firestore document field.

When I query this field I want to show a popup message that the user cannot use the app until the password is changed.

How do I do that in Flutter?

Here is where the login button is clicked

CustomButton(
    onPressed: () {
      if (_formkey.currentState!.validate()) {
        Map<String, dynamic> map = {
          'email': email.text,
          'password': password.text,
        };
        model.login(context: context, map: map);
        // });
      } else {}                         
    },
    text: 'Login',
  ),

When the user enters in their credentials and clicks the "Login" button I navigate to this method

Future<void> login({
    required BuildContext context,
    required Map<String, dynamic> map,
  }) async {
    EasyLoading.show();
    var response = await loginUsecase.call(map);
    response.fold((l) {
      Logger().d('Erroor');
      EasyLoading.dismiss();
    }, (r) {
      // Logger().d(r.toJson());
      // Logger().d(_credential);
      SharedPref.setString(kEMAIL, map[kEMAIL]);
      SharedPref.setString(kPASSWORD, map[kPASSWORD]);
      SharedPref.setBool(kLocalAuthSetted, true);
      EasyLoading.dismiss();

      Get.toNamed(AppRoutes.main);
    });
  }

What I want to do is intercept this method and query the user table called "Users" and check if this user based on the email entered, has the key "ChangePassword" set to true If it is set to true I want to show a message that the password needs to be changed and then re-route them to the forgot password screen.


Solution

  • below i am just trying to pass the logic ,so please use it according to your approach and let me know any.

    db.collection("users").where("email", isEqualTo: 'email').get().then(
      (querySnapshot) {
        print("Successfully completed");
        for (var docSnapshot in querySnapshot.docs) {
          print('${docSnapshot.id} => ${docSnapshot.data()}');
          if (!docSnapshot.data()['changePassword']) {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Text('Password Reset Required'),
                  content: Text('this is your 1st login so you must reset your password before using the app.'),
                  actions: <Widget>[
                    TextButton(
                      onPressed: () {
                        // navigate to password reset screen
                      },
                      child: Text('Reset Password'),
                    ),
                  ],
                );
              },
            );
          }else{
            //docSnapshot.data()['changePassword'] is = true 
            //so he already changed the password
          }
        }
      },
      onError: (e) => print("Error : $e"),
    );
    

    also you can query with both of the field at the same time like below

    db.collection("users").where("email", isEqualTo: 'email')
    .where('changePassword', isEqualTo: false).get().then(
      (querySnapshot) {
    
    //rest as usual 
    
    })
    

    so you need to select what approach is best for you as according to your str.