Home > database >  Check password If its match the old password
Check password If its match the old password

Time:05-02

I want user to type in their password before they delete their acc. And I want the app to validate that password If its match the old password, How can I do it (this is my code so far)

  _deleteAcc(String _password) async {
try {
  await auth.checkActionCode(_password);
  //Success
  FirebaseAuth.instance.currentUser?.delete();
  Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (_) => const LoginScreen()),
      (route) => false);
  Fluttertoast.showToast(
    msg: 'Your account has been deleted',
    gravity: ToastGravity.TOP,
    toastLength: Toast.LENGTH_LONG, //duration 5sec
    backgroundColor: Colors.grey[400],
    textColor: Colors.black,
  );
} on FirebaseAuthException {
  Fluttertoast.showToast(
    msg: 'Password not correct',
    gravity: ToastGravity.TOP,
    toastLength: Toast.LENGTH_LONG, //duration 5sec
    backgroundColor: Colors.grey[400],
    textColor: Colors.black,
  );
}
    Future openDialog() => showDialog(
  context: context,
  builder: (context) => AlertDialog(
        title: const Text('Are you sure? '),
        content: Form(
          autovalidateMode: AutovalidateMode.always,
          child: TextFormField(
            decoration: const InputDecoration(hintText: 'Confirm 
           Password'),
            validator: (value) => validatePassword(value),
            obscureText: false,
            onChanged: (value) {
              setState(() {
                _password = value.trim();
              });
            },
          ),
        ),
        actions: [
          OutlinedButton(
            onPressed: (() => _deleteAcc(_password)),
            child: const Text(
              'Continue',
              style: TextStyle(color: Colors.redAccent, fontSize: 
               17.0),
            ),
            style: OutlinedButton.styleFrom(
              side: const BorderSide(width: 5.0, color: Colors.transparent),
            ),
          ),

Images

The continue button call in the _delete Acc method

CodePudding user response:

You can try to login user,

if it's login details are correct than you can delete account. but if its login details are incorrect then Firebase gives us an exception that login details are not correct. but you have to store user's email ID or usernames in local DB or shared preference. so you can just ask for a password and you have to fetch the email id from the local database or shared preference.

  • Related