【问题标题】:Trying to run authy-client with Firebase Cloud Functions尝试使用 Firebase Cloud Functions 运行 authy-client
【发布时间】:2018-01-31 21:02:12
【问题描述】:

我一直在尝试让 authy-client 与 Firebase Cloud Functions 一起运行,但我一直遇到 ValidationFailedError。我一直在测试作者在https://www.npmjs.com/package/authy-client 提供的示例,但没有成功。

对于我的 Firebase 功能,我一直在尝试:

const Client = require('authy-client').Client;
const client = new Client({ key: 'my API key here' });

exports.sendVerificationCode = functions.database.ref('users/{userId}/verify/status')
.onCreate(event => {
  const sender = client.registerUser({
    countryCode: 'US',
    email: 'test@tester.com',
    phone: '4035555555'
  }).then( response => {
    return response.user.id;
  }).then( authyId => {
    return client.requestSms({ authyId: authyId });
  }).then( response => {
    console.log(`SMS requested to ${response.cellphone}`);
    throw Promise;
  });

  return Promise.all([sender]);
});

但我收到此错误:

ValidationFailedError: Validation Failed
    at validate (/user_code/node_modules/authy-client/dist/src/validator.js:74:11)
    at _bluebird2.default.try (/user_code/node_modules/authy-client/dist/src/client.js:632:31)
    at tryCatcher (/user_code/node_modules/authy-client/node_modules/bluebird/js/release/util.js:16:23)
    at Function.Promise.attempt.Promise.try (/user_code/node_modules/authy-client/node_modules/bluebird/js/release/method.js:39:29)
    at Client.registerUser (/user_code/node_modules/authy-client/dist/src/client.js:617:34)
    at exports.sendVerificationCode.functions.database.ref.onCreate.event (/user_code/index.js:24:25)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
    at /var/tmp/worker/worker.js:695:26
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我是 Firebase 的云函数的新手,所以我可能忽略了一些明显的东西,但从我读过的内容来看,then() 语句和函数本身需要返回/抛出一个 Promise,所以我一起破解了它。

我还确保 authy-client 包含在 package.json 文件的依赖项中。

我最初注册了 Twilio 试用版,它让我可以选择使用 Authy 创建应用程序。需要确定的是,我还登录了 Authy 以检查 API 密钥是否相同,它们是否相同。所以我不认为验证错误是由于 API 密钥造成的。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: firebase twilio google-cloud-functions authy


    【解决方案1】:

    谢谢大家的回答。我终于能够想出一个解决方案。它与代码无关,throw Promise 是一个错误,但显然是 DNS 解析的问题,当我在 Firebase 上设置计费时已解决。

    至于我的最终代码,因为我最初想使用 authy-client 进行电话验证,它看起来像这样:

    exports.sendVerificationCode = functions.database.ref('users/{userId}/verify')
    .onCreate(event => {
      const status = event.data.child('status').val();
      const phoneNum = event.data.child('phone').val();
      const countryCode = event.data.child('countryCode').val();
      var method;
    
      if(status === 'pendingSMS')
        method = 'sms';
      else
        method = 'call';
    
      // send code to phone
      const sender = authy.startPhoneVerification({ countryCode: countryCode, phone: phoneNum, via: method })
      .then( response => {
        return response;
      }).catch( error => {
        throw error;
      });
    
      return Promise.all([sender]);
    });
    

    【讨论】:

      【解决方案2】:

      throw Promise 没有任何意义。如果你想捕获在你的 Promise 序列中任何地方可能出现的问题,你应该有一个 catch 部分。一般形式是这样的:

      return someAsyncFunction()
          .then(...)
          .then(...)
          .catch(error => { console.error(error) })
      

      不过,这不一定能解决您的错误。这可能来自您调用的 API。

      【讨论】:

        【解决方案3】:

        这里是 Twilio 开发者宣传员。

        Doug 对throw Promise 的看法是正确的,那肯定需要改变。

        但是,错误似乎是在此之前并来自 API。具体来说,堆栈跟踪告诉我们:

            at Client.registerUser (/user_code/node_modules/authy-client/dist/src/client.js:617:34)
        

        所以问题出在registerUser 函数中。最好的办法是尝试公开更多从 API 生成的错误。这应该会为您提供所需的信息。

        这样的事情应该会有所帮助:

        const Client = require('authy-client').Client;
        const client = new Client({ key: 'my API key here' });
        
        exports.sendVerificationCode = functions.database.ref('users/{userId}/verify/status')
        .onCreate(event => {
          const sender = client.registerUser({
            countryCode: 'US',
            email: 'test@tester.com',
            phone: '4035555555'
          }).then( response => {
            return response.user.id;
          }).then( authyId => {
            return client.requestSms({ authyId: authyId });
          }).then( response => {
            console.log(`SMS requested to ${response.cellphone}`);
          }).catch( error => {
            console.error(error.code);
            console.error(error.message);
            throw error;
          });    
        });
        

        让我知道这是否有帮助。

        【讨论】:

        • 感谢您的帮助。我终于能够找到解决方案,问题就在我身上。在 Firebase 上设置结算之前,显然不允许来自非 Google 资源的 DNS 解析。
        猜你喜欢
        • 2019-05-18
        • 1970-01-01
        • 2020-10-01
        • 2017-08-07
        • 2021-08-13
        • 2022-12-03
        • 2020-02-21
        • 2021-12-26
        • 2017-09-03
        相关资源
        最近更新 更多