flutter: [ERROR]: CreateAuthChallenge failed with error SyntaxError: await is only valid in async function

0

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

Here is my createAuthChallenge.js

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?

  • I am a bit puzzled since the error message seems to be be generated by the Flutter code and not the lambda function. Can you possibly provide the CloudWatch Logs of the lambda function?

  • Thanks for the comment, I did just the same, and the issue seemed to have been with UserFunctions.js. As the logs were not really helpful beyond that, I deleted amplify and copied the same lambdas, and now seems to work.

1 Answer
0
Accepted Answer

Not entirely sure what the root cause of the error messages above were, but there is nothing wrong with the codes above. Redid everything from amplify delete > amplify init > amplify add auth > add lambdas to local > amplify push And now everything works, most likely some odd mistake that I made without realizing

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions