【问题标题】:Authentication Service Webhook (endpoint) in MongoDB StitchMongoDB Stitch 中的身份验证服务 Webhook(端点)
【发布时间】:2018-11-30 19:30:52
【问题描述】:

有没有办法创建服务 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。我的方向正确吗?

【问题讨论】:

    标签: mongodb webhooks es6-modules mongodb-stitch


    【解决方案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 而不是httpconst http = context.services.get("&lt;SERVICE_NAME&gt;");

    【讨论】:

    • http.post 是undefined。你有什么主意吗?我也启用了邮政服务规则
    • const http = context.services.get("http"); 需要配置为 ServiceName 而不是 httpconst http = context.services.get("&lt;SERVICE_NAME&gt;");
    猜你喜欢
    • 2019-05-02
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 2021-02-27
    • 2020-04-22
    • 2012-08-28
    相关资源
    最近更新 更多