Flutter firebase: reauthentication

Viewed 16

I have a section of an app dedicated to deleting all of a user's data. Firebase auth requires a user to reathenticate before deleting its data, so im trying the following function:

onPressed: () async {
                                                    try {
                                                      AuthCredential
                                                          credential =
                                                          EmailAuthProvider
                                                              .credential(
                                                        email: '',
                                                        password: '',
                                                      );
                                                      _firebaseAuth.currentUser!
                                                          .reauthenticateWithCredential(
                                                              credential);
                                                      BlocProvider.of<
                                                                  ProfileBloc>(
                                                              context)
                                                          .add(DeleteProfile(
                                                        user: user,
                                                        stingrays: stingrayState
                                                            .stingrays,
                                                      ));

                                                      Navigator.pushNamed(
                                                          context, '/wrapper/');
                                                    } on Exception catch (e) {
                                                      print(e.toString());
                                                    }
                                                  },

In this case, setting the email and password as '' should throw an error. My current idea is that the function should run until reauthenciation is called, then an error should be thrown and it does not finish the deleting process. However, it currently does not operate like this, instead running through the entire function. Any idea how to fix this conditional reauthentication method?

Thank you!

1 Answers

You can just call return whenever you want your code to stop and it will.

So after your authentication do this.

_firebaseAuth.currentUser!.reauthenticateWithCredential(credential);
return;

Also reauthenticateWithCredential() is a Future<void> so you have to await it:

return await _firebaseAuth.currentUser!.reauthenticateWithCredential(credential);
Related