使用MongoDB Stitch自定义身份验证的Firebase身份验证:如何将用户信息从Firebase传递到MongoDB

时间:2020-05-12 09:52:12

标签: swift mongodb firebase firebase-authentication mongodb-stitch

我正在将Firebase身份验证与Swift iOS上的MongoDB Stitch自定义身份验证集成在一起,并希望将用户的信息从Firebase传递到mongodb,以便用户的电子邮件或姓名可以在此处的MongoDB控制台上显示:

enter image description here

基于MongoDB的文档,可以从JWT通过metadata访问这些文档,但是我不知道如何从iOS添加它们。这就是我从Firebase到Stitch进行身份验证的方式:

Auth.auth().signIn(with: credential) { (results, error) in
        if let error = error {
            print(error.localizedDescription)
            return
        }

        if let results = results {
            results.user.getIDTokenForcingRefresh(true) { (tokenId, error) in
                if let error = error {
                    completion(error)
                }

                if let tokenId = tokenId {
                    let credential = CustomCredential.init(withToken: tokenId)
                    stitch.auth.login(withCredential: credential) { (result) in
                        switch result {
                        case .success(let user):
                            print("Sign in to Stitch successful")
                        case .failure(let e):
                            print(e)
                        }
                    }
                }
            }
        }
    }
}

以上身份验证方法有效,我可以在Stitch上成功进行身份验证。但是,如何从Firebase提取提供程序数据并将其显示在MongoDB上?

1 个答案:

答案 0 :(得分:0)

您必须在“用户”>“提供者”>“自定义JWT身份验证”下配置提供者,然后相应地映射字段。

此图片

enter image description here

将映射如下字段:

enter image description here