0

有没有办法创建一个服务 webhook 来使用电子邮件和密码注册新用户?

我可以通过 SDK 看到方式,但我试图通过服务 webhook 功能做同样的事情?

例如

exports = function(payload) {

 const { Stitch, AnonymousCredential } = require('mongodb-stitch-server-sdk');

  var queryArg = payload.query || '';
  var body = {};

  if (payload.body) {
  body = EJSON.parse(payload.body.text());
  }

  return body.email;
};

我无法访问mongodb-stitch-server-sdk这里。我是否朝着正确的方向前进?

4

1 回答 1

1

因此,您将无法在 webhook 中使用 SDK。您可以通过点击 Stitch Admin API 添加用户。

  1. 在 Atlas 中创建 API 密钥。转到右上角的用户下拉菜单 > 帐户 > 公共 API 访问。单击“生成”,然后保存创建的 API 密钥。

  2. 在 Stitch 中创建一个 HTTP 服务。

  3. 在您的 webhook 中,使用Admin API进行身份验证并创建新用户。代码将类似于:

    exports = function(payload) {
        const http = context.services.get("http");
        return http.post({
            url: "https://stitch.mongodb.com/api/admin/v3.0/auth/providers/mongodb-cloud/login",
            body: JSON.stringify({ 
                username: "<atlas-username>",
                apiKey: "<atlas-apiKey>"
            })
        }).then(response => EJSON.parse(response.body.text()).access_token).then(accessToken => {
            return http.post({
                url: "https://stitch.mongodb.com/api/admin/v3.0/groups/<groupId>/apps/<appId>/users",
                headers: {
                    Authorization: ["Bearer " + accessToken]
                },
                body: JSON.stringify({ 
                    email: "<email-from-payload>",
                    password: "<password-from-payload>"
                })
            });
        });
    };
    

评论后:

const http = context.services.get("http");需要配置ServiceName而不是httpasconst http = context.services.get("<SERVICE_NAME>");

于 2018-11-29T18:49:25.710 回答