Last active
July 24, 2024 06:53
-
-
Save alsoamit/b7dcd800cac857911f51e87813cb4eb2 to your computer and use it in GitHub Desktop.
Cognito saveUserDetailsPostConfirmation appsync integration lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UPDATED for Node 20.x + and aws-sdk v3 | |
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; | |
const REGION = process.env.REGION; | |
const USERTABLE = process.env.USERTABLE; | |
const ddbClient = new DynamoDBClient({ region: REGION }); | |
export const handler = async (event, context) => { | |
let date = new Date(); | |
if (event.request.userAttributes.sub) { | |
const createdAtISO8601 = new Date().toISOString(); | |
const updatedAtISO8601 = new Date().toISOString(); | |
const params = { | |
TableName: USERTABLE, | |
Item: { | |
'sub': { S: event.request.userAttributes.sub }, | |
'__typename': { S: 'CognitoUser' }, | |
'username': { S: event.request.userAttributes.sub }, | |
'email': { S: event.request.userAttributes.email }, | |
'createdAt': { S: createdAtISO8601 }, // ISO 8601 formatted timestamp for createdAt | |
'updatedAt': { S: updatedAtISO8601 }, // ISO 8601 formatted timestamp for updatedAt | |
'gender': { S: 'male' }, | |
'name': { S: event.request.userAttributes.name } | |
} | |
}; | |
console.log({ params, event: event.request.userAttributes }); | |
try { | |
await ddbClient.send(new PutItemCommand(params)); | |
console.log("Success"); | |
} catch (err) { | |
console.log("Error", err); | |
} | |
console.log("Success: Everything executed correctly"); | |
return event; | |
} else { | |
console.log("Error: Nothing was written to DynamoDB"); | |
return event; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var aws = require('aws-sdk'); | |
var ddb = new aws.DynamoDB({ | |
region: process.env.REGION | |
}); | |
exports.handler = async (event, context) => { | |
let date = new Date(); | |
if (event.request.userAttributes.sub) { | |
let params = { | |
Item: { | |
'sub': {S: event.request.userAttributes.sub}, | |
'__typename': {S: 'CognitoUser'}, | |
'username': {S: event.userName}, | |
'email': {S: event.request.userAttributes.email}, | |
'createdAt': {S: date.toISOString()}, | |
'updatedAt': {S: date.toISOString()}, | |
'gender': {S: 'male'}, | |
'name': {S: event.request.userAttributes.name}, | |
}, | |
TableName: process.env.USERTABLE | |
}; | |
let ownershipParams = { | |
Item: { | |
'sub': {S: event.request.userAttributes.sub}, | |
}, | |
TableName: process.env.OWNERSHIP_TABLE | |
}; | |
console.log({params, event: event.request.userAttributes}); | |
try { | |
await ddb.putItem(params).promise(); | |
await ddb.putItem(ownershipParams).promise(); | |
console.log("Success"); | |
} catch (err) { | |
console.log("Error", err); | |
} | |
console.log("Success: Everything executed correctly"); | |
context.done(null, event); | |
} else { | |
console.log("Error: Nothing was written to DynamoDB"); | |
context.done(null, event); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DynamoDBClient, PutItemCommand, GetItemCommand } from "@aws-sdk/client-dynamodb"; | |
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb"; | |
const REGION = process.env.REGION; | |
const USERTABLE = process.env.USERTABLE; | |
const TEAMTABLE = process.env.TEAMTABLE; | |
const MEMBERSTABLE = process.env.MEMBERSTABLE; | |
const ddbClient = new DynamoDBClient({ region: REGION }); | |
export const handler = async (event, context) => { | |
console.log({ event, context }); | |
let date = new Date(); | |
// Check the triggerSource to ensure it is PostConfirmation_ConfirmSignUp | |
if (event.triggerSource === 'PostConfirmation_ConfirmSignUp') { | |
const userAttributes = event.request.userAttributes; | |
// Check if "custom:teamId" is present in user attributes | |
if (userAttributes['custom:teamId'] !== 'any') { | |
const teamId = userAttributes['custom:teamId']; | |
const email = userAttributes.email; | |
const userId = userAttributes.sub; | |
// Get team information from DynamoDB | |
const getTeamParams = { | |
TableName: TEAMTABLE, | |
Key: { | |
'id': { S: teamId } | |
} | |
}; | |
try { | |
const { Item } = await ddbClient.send(new GetItemCommand(getTeamParams)); | |
console.log({Item}) | |
if (Item) { | |
const team = unmarshall(Item); | |
console.log({team, invitations: team.invitations}); | |
// Check if the invitations array exists and is an array | |
if (team.invitations && Array.isArray(team.invitations)) { | |
// Check if the email is in any of the invitations | |
const isInvited = team.invitations.some(invitation => invitation.email && invitation.email === email); | |
console.log({isInvited, some: team.invitations.some(invitation => invitation.email && invitation.email === email)}) | |
if (isInvited) { | |
// Perform an action if the email is found in the invitations array | |
console.log(`Email ${email} found in team invitations for team ${teamId}`); | |
// Add an entry to MembersTable | |
const putMemberParams = { | |
TableName: MEMBERSTABLE, | |
Item: marshall({ | |
'teamId': teamId, | |
'userId': userId | |
}) | |
}; | |
try { | |
await ddbClient.send(new PutItemCommand(putMemberParams)); | |
console.log(`User ${userId} added to MembersTable for team ${teamId}`); | |
// Update user attributes | |
userAttributes.step = "7"; | |
userAttributes.onboarded = "false"; | |
userAttributes.invitedByTeam = teamId; | |
} catch (err) { | |
console.log("Error adding user to MembersTable:", err); | |
} | |
} | |
} | |
else { | |
userAttributes.step = "1"; | |
userAttributes.onboarded = "false"; | |
userAttributes.invitedByTeam = 'none'; | |
} | |
} | |
} catch (err) { | |
console.log("Error fetching team from DynamoDB:", err); | |
} | |
} else { | |
// If "custom:teamId" is not present, set step to its default value and onboarded to false | |
userAttributes.step = "1"; | |
userAttributes.onboarded = "false"; | |
userAttributes.invitedByTeam = 'none'; | |
} | |
console.log({userAttributes}) | |
// Proceed with inserting/updating the user into DynamoDB | |
const params = { | |
TableName: USERTABLE, | |
Item: marshall({ | |
'sub': userAttributes.sub, | |
'__typename': 'CognitoUser', | |
'username': event.userName, | |
'email': userAttributes.email, | |
'onboarded': userAttributes.onboarded, | |
'step': userAttributes.step, | |
'gender': 'rns', | |
'status': 'active', | |
'resultPrivacy': false, | |
'stripeCustomerId': userAttributes['custom:stripeCustomerId'], | |
'createdAt': date.toISOString(), | |
'updatedAt': date.toISOString(), | |
'invitedByTeam': userAttributes.invitedByTeam, | |
}) | |
}; | |
console.log({params}) | |
try { | |
await ddbClient.send(new PutItemCommand(params)); | |
console.log("User inserted/updated into DynamoDB successfully"); | |
} catch (err) { | |
console.log("Error inserting/updating user into DynamoDB:", err); | |
} | |
console.log("Success: Everything executed correctly"); | |
return event; | |
} else { | |
console.log("Error: Trigger source is not PostConfirmation_ConfirmSignUp"); | |
return event; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment