使用aws-sdk在phone_number上发送OTP验证短信

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

我在 aws 中创建了一个用户池,用于使用电话号码进行 otp 验证。

下面是发送 OTP 消息的代码片段。

import { CognitoIdentityServiceProvider } from "aws-sdk";

const cognito = new CognitoIdentityServiceProvider({
  region: "ca-central-1",
});

const userPoolId = "ca-central-xxx";
const appClientId = "xxx";

export async function sendOTP(phoneNumber: string, password: string) {
  const params = {
    ClientId: appClientId,
    Username: phoneNumber,
    Password: password,
    UserAttributes: [
      {
        Name: "phone_number",
        Value: phoneNumber,
      },
    ],
  };

  return cognito.signUp(params).promise();
}

当我调用该函数时,我会在响应中得到这个。

{
  UserConfirmed: false,
  CodeDeliveryDetails: {
    Destination: '+********2004',
    DeliveryMedium: 'SMS',
    AttributeName: 'phone_number'
  },
  UserSub: '8cdd9528-a001-70fc-f556-xxxx'
}

对我来说,我似乎能够成功发送 otp 消息,但我的电话号码上没有收到任何验证码。 我尝试对此进行调试,创建并删除了具有不同 IAM 角色的多个 user_pool,但没有任何效果。

我确信 aws 未能发送 OTP,因为这是控制台中显示的内容。

在上图中,您可以看到一些 OTP 验证消息已成功发送,但这最初发生。后来就停止工作了。

我需要帮助来调试此问题,可能的原因是什么?

注意:我处于沙盒模式而不是生产环境,是的,我正在通过经过验证的电话号码发送 OTP。

node.js amazon-web-services amazon-cognito aws-sdk
1个回答
0
投票

我找到了解决方案。 我使用的是发送 OTP 的旧方法。 AWS 更新了他们的 sdk,我使用

@aws-sdk/client-cognito-identity-provider
代替,它成功了。

© www.soinside.com 2019 - 2024. All rights reserved.