简体   繁体   中英

Flutter firebase: reauthentication

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!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM