How to delete firebase account when user data is deleted on flutter?

◇◆丶佛笑我妖孽 提交于 2020-05-16 03:18:11

问题


is it possible to delete firebase account in authentication on flutter? if yes, how to do that? I have been search but not found the way.

Firestore.instance.collection("users").document(uid).delete().then((_){
   // delete account on authentication after user data on database is deleted            
});

回答1:


To delete a user account, call delete() on the user object.

For more on this, see the reference documentation for FirebaseUser.delete().




回答2:


Code for deleting user:

FirebaseUser user = await FirebaseAuth.instance.currentUser();
user.delete();




回答3:


Using flutter, if you want to delete firebase accounts together with the associated firestore user collection document, the following method works fine. (documents in user collection named by the firebase uid).

Database Class

class DatabaseService {
  final String uid;

  DatabaseService({this.uid});

  final CollectionReference userCollection =
      Firestore.instance.collection('users');

  Future deleteuser() {
    return userCollection.document(uid).delete();
  }
}

Use Firebase version 0.15.0 or above otherwise, Firebase reauthenticateWithCredential() method throw an error like { noSuchMethod: was called on null }.

Authentication Class

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

Future deleteUser(String email, String password) async {
    try {
      FirebaseUser user = await _auth.currentUser();
      AuthCredential credentials =
          EmailAuthProvider.getCredential(email: email, password: password);
      print(user);
      AuthResult result = await user.reauthenticateWithCredential(credentials);
      await DatabaseService(uid: result.user.uid).deleteuser(); // called from database class
      await result.user.delete();
      return true;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

Then use the following code inside the clickable event of a flutter widget tree to achieve the goal;

onTap: () async {  
     await AuthService().deleteUser(email, password);
}


来源:https://stackoverflow.com/questions/56436734/how-to-delete-firebase-account-when-user-data-is-deleted-on-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!