Swift Firebase在删除时触发Cloud Function [英] Swift Firebase trigger Cloud Function on delete

查看:65
本文介绍了Swift Firebase在删除时触发Cloud Function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在内部使用删除触发器函数我的项目中,我首先从身份验证中删除用户,然后从数据库中删除用户.

I would like to use the on delete trigger function inside my project where I first delete the user from authentication and after that delete the user from the database.

在提供的链接中是一个示例,但我无法使其在Swift中工作.假设我可以使其正常运行,我可以这样称呼吗?还是Auth.auth().currentUser不再可用?对此有什么解决方法吗?

In the provided link is a example, but I can not make it work in Swift. Let's say I can make it work, can I call something like this? Or is Auth.auth().currentUser not available anymore? And is there some workaround for that?

Auth.user.onDelete() { 
    let db = Firestore.firestore()
    let user = Auth.auth().currentUser
    db.collection("users").document(user).delete() { err in
        if err != nil {
            Utilities.showErrorPopUp(labelContent: "Fehler", description: err!.localizedDescription)
            finished(false)
        } else {
            finished(true)
        }
    }
}

我知道我可以先从数据库中删除用户,然后再从身份验证中删除用户,但这并不适合我的用例.有人可以帮我从这里出去吗?另外,请让我知道是否有任何不清楚的地方,以及是否需要更多信息.

I know I could delete the user from the database first and from authentication afterwards but that is not quite fitting for my use-case. Can anyone help me out here? Also, let me know if anything is unclear and if you need more information.

推荐答案

我认为您可能在这里混淆了一些事情.

I think you may be confusing a few thing here.

Cloud Functions是用Node.js编写的,并在Google服务器上运行-从而使它们能够响应Firebase项目中发生的事件.您无法在客户端Swift代码中响应所有相同的事件,例如删除用户帐户.因此,尽管使用Cloud Functions从Cloud Firestore删除文档以响应从Firebase身份验证删除用户帐户是完全可行的,但是您不能在客户端Swift代码中执行此操作.

Cloud Functions are written in Node.js, and run on Google servers - which allows them to respond to events happening in your Firebase projects. You can't respond to all those same events, such as a user account being deleted, in your client-side Swift code. So while it is perfectly feasible to use Cloud Functions to delete a document from Cloud Firestore in response to a user account being deleted from Firebase Authentication, you can't do this in your client-side Swift code.

您可以在Swift代码中执行的操作是删除当前登录的用户帐户,然后从Firestore中删除其文档.这样做的典型顺序是先删除文档,然后再删除帐户,因为删除文档可能/应​​该要求用户仍然存在并且需要进行身份验证.

What you can do in your Swift code, is delete the currently logged in user's account, and delete their document from Firestore. The typical order to do that in is to first delete the document, and only then delete the account, as deleting the document probably/should require the user to still exist and be authentication.

类似于:

db.collection("cities").document("DC").delete() { err in
    if let err = err {
        print("Error removing document: \(err)")
    } else {
        print("Document successfully removed!")
        let user = Auth.auth().currentUser
    
        user?.delete { error in
          if let error = error {
            // An error happened.
          } else {
            // Account deleted.
          }
        }
    }
}

以上代码大多是从删除中的文档中以1:1复制的来自Firestore的数据从Firebase身份验证中删除用户.

这篇关于Swift Firebase在删除时触发Cloud Function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆