Firebase user.delete方法有效但出现错误

问题描述

我正在使用angular,并且有一个存储用户详细信息和登录信息的应用程序。尝试删除用户时,我首先要删除所有与用户相关的信息。然后要求用户重新进行身份验证,身份验证后,用户将注销,并获取其基本详细信息以显示个人资料ID删除,然后使用user.delete()登录信息。

所有这些都按预期工作,但是最后我得到了一个错误。即使我已经注销了应用程序的用户,为什么我仍然收到此错误。

错误消息:{code: "auth/user-token-expired",message: "The user's credential is no longer valid. The user must sign in again.",a: null}

我的代码-

  deleteAccount(){
    var userToDelete = firebase.auth().currentUser;
    this.logout();
    this.store.dispatch(UI.StartAppLoad({status:'Deleting User Details...'}));

    this.userService.DeleteUser(userToDelete.uid)
    .then((res)=>{
      console.log(res);
    }).catch(this.HandleError.bind(this));

    userToDelete.delete().then(
      (res)=>{
        console.log(res);
        this.uiService.showSnackbar('User Account Deleted',null,3000);
        this.store.dispatch(UI.LoadApp());
      }
    ).catch(this.HandleError.bind(this));
  }

  logout() {
    this.afAuth.signOut();
  }

其中HandleError用于在小吃栏中显示错误消息。

deleteAccount()在用户成功进行身份验证之后被调用。

我不想显示错误消息,而是要显示消息'User Account Deleted'

整个流程-

  onDeleteAccount(){
    const confirmResult = this.uiService.showConfirm({
      isDanger:true,title:'Delete Account?',content:'All your user account data will be permamnently deleted.'+
              ' You will need to create a new account later. Are you sure you want to continue?',okText:'Delete'
    });
    confirmResult.subscribe(async isDelete=>{
      if(isDelete){
        this.store.dispatch(UI.StartAppLoad({status:'Deleting Excercise Data...'}));
        const isResetDone = await this.trainingService.resetPastExercise();
        if(isResetDone){
          this.store.dispatch(UI.StartAppLoad({status:'Deleting Follow list...'}));
          this.userService.clearFollowList();
          this.authService.actionToPerform.next(actions.Delete_Account);
          this.store.dispatch(UI.LoadApp());
          this.router.navigate([AppRoutes.ReAuthenticate]);
        }
      }
    });
  }

验证页面的submit()方法:

this.authService.reauthenticate({
  email:form.value.email,password:form.value.password
});

this.authService.deleteAccount();

AuthService:

reauthenticate(authdata: AuthData) {
    this.store.dispatch(UI.StartLoading());
    var credential = firebase.auth.EmailAuthProvider.credential(
      authdata.email,authdata.password
    );
    this.afAuth.currentUser.then((user) => {
      user.reauthenticateWithCredential(credential)
        .then((res)=>{
          this.prevPwd = authdata.password;
          console.log(res);
          this.store.dispatch(UI.StopLoading());
        })
        .catch(this.HandleError.bind(this))
    });
  }

然后使用上述方法deleteAccount()

请提出建议。

解决方法

doc中所述,发生这种情况的原因是:

delete()是一项对安全性敏感的操作,要求用户执行以下操作: 最近登录了

该文档还表明:

如果不满足此要求,请询问用户 再次进行身份验证,然后致电firebase.User.reauthenticateWithCredential

因此,您需要处理此特定错误并按照文档指示执行操作,即“要求用户再次进行身份验证,然后致电reauthenticateWithCredential。”

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...