All files / auth post-confirmation.js

100% Statements 15/15
100% Branches 2/2
100% Functions 1/1
100% Lines 15/15

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 431x 1x   1x 1x           1x 8x   8x 8x 8x 8x     8x                         6x     6x   2x   2x      
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, PutCommand } = require('@aws-sdk/lib-dynamodb')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
/**
 * Cognito Post Confirmation Lambda Trigger
 * Creates user record in UsersTable after successful Cognito signup confirmation
 */
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 name = event.request.userAttributes.name || email.split('@')[0]
 
    // Create user in UsersTable
    await docClient.send(
      new PutCommand({
        TableName: process.env.USERS_TABLE_NAME,
        Item: {
          id: userId,
          email: email,
          name: name,
          status: 'active',
          createdAt: new Date().toISOString()
        }
      })
    )
 
    console.log(`User ${userId} created in UsersTable`)
 
    // 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
  }
}