forked from nash-md/twilio-verify-example
-
Notifications
You must be signed in to change notification settings - Fork 2
/
challenge-manager.js
62 lines (48 loc) · 1.42 KB
/
challenge-manager.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
const twilio = require('twilio');
class ChallengeManager {
constructor() {
this.client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
this.challenges = new Map();
}
async create(user, fields) {
console.log('identity: ', user.id);
const challenge = await this.client.verify
.services(process.env.TWILIO_VERIFY_SERVICE_SID)
.entities(user.id)
.challenges.create({
'details.message': 'Please approve the login request',
'details.fields': fields,
factorSid: user.factor.sid
});
this.challenges.set(challenge.sid, {
status: challenge.status,
socket: undefined
});
return challenge;
}
async fetch(user, sid) {
const challenge = await this.client.verify
.services(process.env.TWILIO_VERIFY_SERVICE_SID)
.entities(user.id)
.challenges(sid)
.fetch();
this.challenges.set(challenge.sid, {
status: challenge.status,
socket: undefined
});
return challenge;
}
get(sid) {
return this.challenges.get(sid);
}
update(sid, status) {
const { socket } = this.challenges.get(sid);
this.challenges.set(sid, { socket, status });
socket && socket.send(JSON.stringify({ status: status }));
}
registerSocket(sid, socket) {
const { status } = this.challenges.get(sid);
this.challenges.set(sid, { status, socket });
}
}
module.exports = ChallengeManager;