aws-lambda#CreateAuthChallengeTriggerEvent TypeScript Examples

The following examples show how to use aws-lambda#CreateAuthChallengeTriggerEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: createAuthChallenge.ts    From office-booker with MIT License 5 votes vote down vote up
createAuthChallengeHandler = async (event: CreateAuthChallengeTriggerEvent) => {
  try {
    const domain = process.env.DOMAIN;
    const fromAddress = process.env.FROM_ADDRESS;
    if (typeof domain !== 'string') {
      throw new Error('No DOMAIN defined in ENV');
    }
    if (typeof fromAddress !== 'string') {
      throw new Error('No FROM_ADDRESS defined in ENV');
    }

    let secretLoginCode: string;

    if (!event.request.session || !event.request.session.length) {
      // This is a new auth session
      // Generate a new secret login code and mail it to the user
      secretLoginCode = randomDigits(6).join('');

      await sendEmail(domain, event.request.userAttributes.email, secretLoginCode, fromAddress);
    } else {
      // There's an existing session. Don't generate new digits but
      // re-use the code from the current session. This allows the user to
      // make a mistake when keying in the code and to then retry, rather
      // the needing to e-mail the user an all new code again.
      const previousChallenge = event.request.session.slice(-1)[0];

      secretLoginCode = previousChallenge.challengeMetadata!.match(/CODE-(\d*)/)![1];
    }

    // This is sent back to the client app
    event.response.publicChallengeParameters = { email: event.request.userAttributes.email };

    // Add the secret login code to the private challenge parameters
    // so it can be verified by the "Verify Auth Challenge Response" trigger
    event.response.privateChallengeParameters = { secretLoginCode };

    // Add the secret login code to the session so it is available
    // in a next invocation of the "Create Auth Challenge" trigger
    event.response.challengeMetadata = `CODE-${secretLoginCode}`;

    return event;
  } catch (error) {
    console.error(JSON.stringify({ level: 'ERROR', error: error.message + '\n' + error.stack }));
    throw error;
  }
}