MongoDB Stitch中的身份验证服务Webhook(端点)



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

因此,您将无法在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,而不是http作为const http = context.services.get("<SERVICE_NAME>");

最新更新