-
Notifications
You must be signed in to change notification settings - Fork 0
/
resend_email_confirmation.js
72 lines (59 loc) · 1.72 KB
/
resend_email_confirmation.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const aws = require('aws-sdk');
const BaseEmailConfirmationUrl = process.env.salgode_email_confirmation_base_url;
const UsersTableName = process.env.dynamodb_users_table_name;
const dynamoDB = new aws.DynamoDB.DocumentClient();
const ses = new aws.SES();
function parseConfirmationUrl(userId, token) {
return `${BaseEmailConfirmationUrl}?user=${userId}&token=${token}`;
}
async function getUser(userId) {
const params = {
TableName: UsersTableName,
Key: {
user_id: userId
},
ProjectionExpression: 'first_name, email, email_token'
};
const data = await dynamoDB.get(params).promise();
return data.Item;
}
async function sendConfirmationEmail(recipientAddress, recipientName, confirmationLink) {
const params = {
Destination: {
ToAddresses: [recipientAddress]
},
Message: {
Subject: { Data: 'Bienvenid@ a SALGODE - Confirma tu email' },
Body: {
Text:
{
Data:
`Hola ${recipientName}!
List@ para hacer del mundo un lugar mejor?
Confirma tu email con este link ${confirmationLink}
Atentamente,
Equipo #SalgoDe`
}
}
},
Source: '[email protected]'
};
const result = await ses.sendEmail(params).promise();
return result;
}
exports.handler = async (event) => {
const userId = event.requestContext.authorizer.user_id;
const user = await getUser(userId);
const confirmationUrl = parseConfirmationUrl(userId, user.email_token);
await sendConfirmationEmail(user.email, user.first_name, confirmationUrl);
return {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
action: 'resend email',
success: true,
resource: 'user',
resource_id: userId
})
};
};