-
Notifications
You must be signed in to change notification settings - Fork 1
/
cognito-verify-user.js
94 lines (86 loc) · 2.3 KB
/
cognito-verify-user.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const AWSCognito = require("amazon-cognito-identity-js");
const session = ({ Email, Code, UserPoolId, ClientId }) =>
new Promise((resolve, reject) => {
var poolData = {
UserPoolId: UserPoolId,
ClientId: ClientId,
};
var userPool = new AWSCognito.CognitoUserPool(poolData);
var userData = {
Username: Email,
Pool: userPool,
};
var cognitoUser = new AWSCognito.CognitoUser(userData);
cognitoUser.confirmRegistration(Code, true, function (err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
module.exports = function (RED) {
function verifyUser(config) {
RED.nodes.createNode(this, config);
var Email = this.credentials.email;
var Code = this.credentials.code;
var node = this;
node.on("input", function (msg) {
if (msg.hasOwnProperty("email") || msg.hasOwnProperty("code")) {
Email = msg.email;
delete msg.email;
Code = msg.code;
delete msg.code;
}
const UserPoolId = RED.util.evaluateNodeProperty(
config.userpoolid,
config.userpoolidType,
node,
msg
);
const ClientId = RED.util.evaluateNodeProperty(
config.clientid,
config.clientidType,
node,
msg
);
try {
node.status({
fill: "yellow",
shape: "ring",
text: "verifying user...",
});
session({
Email,
Code,
UserPoolId,
ClientId,
})
.then((result) => {
node.status({
fill: "green",
shape: "ring",
text: "user verified",
});
msg.payload = JSON.stringify(result);
node.send([msg, null]);
})
.catch((error) => {
msg.payload = error;
node.send([null, msg]);
node.status({ fill: "red", shape: "ring", text: error.message });
});
} catch (error) {
msg.payload = error;
node.send([null, msg]);
this.status({ fill: "red", shape: "ring", text: "error" });
}
});
}
RED.nodes.registerType("cognito-verify-user", verifyUser, {
credentials: {
email: { type: "text" },
code: { type: "text" },
},
});
};