All files / auth post-confirmation.js

100% Statements 18/18
100% Branches 0/0
100% Functions 1/1
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521x 1x 1x 1x   1x 1x             1x 18x   18x 18x 18x 18x           18x             16x     16x                 16x   2x   2x      
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, PutCommand } = require('@aws-sdk/lib-dynamodb')
const { syncUserSearchIndex } = require('../utils/directorySearchIndex')
const { buildNewUserItem } = require('../utils/newUserItem')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
/**
 * Cognito Post Confirmation Lambda Trigger
 * Creates user record in UsersTable after successful Cognito signup confirmation.
 * Nickname reservation happens separately, after login, via the set-nickname Lambda.
 */
exports.handler = async (event) => {
  console.log('Post Confirmation Event:', JSON.stringify(event, null, 2))
 
  try {
    const userId = event.request.userAttributes.sub
    const email = event.request.userAttributes.email
    const userItem = buildNewUserItem({
      id: userId,
      email,
      name: event.request.userAttributes.name
    })
 
    await docClient.send(
      new PutCommand({
        TableName: process.env.USERS_TABLE_NAME,
        Item: userItem
      })
    )
 
    console.log(`User ${userId} created in UsersTable`)
 
    // Best-effort: makes the new user findable by search (invites, admin). Never throws.
    await syncUserSearchIndex(
      docClient,
      process.env.FIELD_VALUE_INDEX_TABLE_NAME,
      userId,
      null,
      userItem
    )
 
    // Return event to Cognito (required)
    return event
  } catch (error) {
    console.error('Error in post confirmation:', error)
    // Don't throw error to avoid blocking user signup
    return event
  }
}