Home > Blockchain >  AWS AmplifyAUTH: flutter: [ERROR]: CreateAuthChallenge failed with error SyntaxError: await is only
AWS AmplifyAUTH: flutter: [ERROR]: CreateAuthChallenge failed with error SyntaxError: await is only

Time:01-25

Getting the captioned error message onTap signUpButton() within AuthenticatorForm(). Seems like a very idiomatic error message, but can't seem to find what's wrong.

Here is my createAuthChallenge.js

const AWS = require('aws-sdk');
const digitGenerator = require('crypto-secure-random-digit');

function sendSMS(phone, code) {
  const params = {
    Message: code,
    PhoneNumber: phone,
  };
  return new AWS.SNS({apiVersion: '2010-03-31'}).publish(params).promise();
}

async function createAuthChallenge(event) {
  if (event.request.challengeName === 'CUSTOM_CHALLENGE') {
    const randomDigits = digitGenerator.randomDigits(6).join('');
    const challengeCode = String(randomDigits).join('');
    await sendSMS(event.request.userAttributes.phone_number, challengeCode);
  
    event.response.privateChallengeParameters = {};
    event.response.privateChallengeParameters.answer = challengeCode;
  }
}

exports.handler = async (event) => {
  createAuthChallenge(event);
};

And my package.json for the same

{
  "name": "XYZ",
  "version": "2.0.0",
  "description": "Lambda function generated by Amplify",
  "main": "index.js",
  "license": "Apache-2.0",
  "devDependencies": {
    "@types/aws-lambda": "^8.10.92"
  },
  "dependencies": {
    "crypto-secure-random-digit": "^1.0.9"
  }
}

I can't seem to find the right solution for this, can anyone help please?

CodePudding user response:

Please try to replace your last line with:

exports.handler = createAuthChallenge;

If it doesn't work, can you add some other details?

CodePudding user response:

Not really a useful answer, but as self-resolved.

Ultimately just used the same codes that I copied above, but redid everything from amplify delete > amplify init > amplify add auth > amplify push. After this, no errors in simulator.

If anyone runs into similar problems, the best approach based off my experience on this, is to check the cloudwatch logs of the lambdas fired. In most cases, the logs should do a better job of pointing out where the issue is than the error messages in local. If that doesn't work, backup everything and reset amplify.

  • Related