在Atlas集群中使用MongoDB Stitch App处理用户

问题描述 投票:0回答:1

我有一个MongoDB Stitch应用程序,用户可以通过电子邮件/密码验证。这将在Stitch App中创建我可以在页面上进行身份验证的用户。我的数据库也有一个MongoDB Atlas Cluster。在集群中,我有一个带有项目名称的数据库,然后是“匹配”下面的集合。因此,当我将“匹配”插入集合时,我可以从Stitch发送经过身份验证的用户ID,这样我就可以查询特定用户的所有匹配项。但是如何在针脚中为“用户”集合添加其他值?该用户部分是在Stitch中预先打包的,具有您选择的任何身份验证类型(电子邮件/密码)。但对于我的应用程序,我希望能够在“用户”集合中存储类似“MatchesWon”或“GamePreference”字段的内容。

我是否应该为“用户”创建一个集合,就像我在集群中为“匹配”创建一样,只需插入Stitch提供的用户ID并处理该集合中的字段?好像我会复制用户数据,但我不确定我是否理解另一种方法。还在学习,我感谢任何反馈/建议。

reactjs mongodb mongodb-atlas mongodb-stitch mongodb-cluster
1个回答
2
投票

目前没有办法将您自己的数据存储在内部用户对象上。相反,您可以使用身份验证触发器来管理用户。以下片段取自这些docs

exports = function(authEvent){
   // Only run if this event is for a newly created user.
   if (authEvent.operationType !== "CREATE") { return }

   // Get the internal `user` document
   const { user } = authEvent;

   const users = context.services.get("mongodb-atlas")
       .db("myApplication")
       .collection("users");

   const isLinkedUser = user.identities.length > 1;

   if (isLinkedUser) {
        const { identities } = user;
        return users.updateOne(
            { id: user.id },
            { $set: { identities } }
        )

    } else {
        return users.insertOne({ _id: user.id, ...user })
             .catch(console.error)
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.