-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
66 lines (54 loc) · 1.88 KB
/
Copy pathbasic.js
File metadata and controls
66 lines (54 loc) · 1.88 KB
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
'use strict';
/**
* Minimal end-to-end example.
*
* MXLABS_API_KEY=xxx node examples/basic.js +60123456789
*
* In a real app the `authUrl` must be opened on the USER'S device over its
* cellular data connection (that network path is what silently proves the
* number). Here we just print it so you can open it on a phone and watch the
* poll resolve.
*/
const { MXNumberVerify } = require('../src');
async function main() {
const phone = process.argv[2];
if (!phone) {
console.error('usage: node examples/basic.js <phone>');
process.exit(1);
}
const nv = new MXNumberVerify({
apiKey: process.env.MXLABS_API_KEY,
// backendUrl defaults to the MX Labs hosted backend; override for self-host.
});
await nv.ping();
console.log('backend reachable ✓');
const { sessionId, authUrl } = await nv.start(phone);
console.log('\nOpen this URL on the phone over CELLULAR data:\n ' + authUrl + '\n');
const result = await nv.pollVerify(sessionId, { timeoutMs: 60000 });
console.log('verify result:', result);
if (result.status === 'verified') {
console.log(`\n✓ ${result.phone} verified via ${result.method}`);
return;
}
// Fallback: SMS OTP
console.log('\nSilent verification unavailable — sending SMS OTP…');
const sent = await nv.smsSend(sessionId);
console.log(`OTP sent to ${sent.masked}, expires in ${sent.expires_in}s`);
// In a real app you'd collect the code from the user. Demo reads stdin:
const code = await prompt('Enter the 6-digit code: ');
const otp = await nv.smsVerify(sessionId, code.trim());
console.log('otp result:', otp);
}
function prompt(q) {
return new Promise((resolve) => {
process.stdout.write(q);
process.stdin.once('data', (d) => resolve(d.toString()));
});
}
main().then(
() => process.exit(0),
(err) => {
console.error('error:', err.code || '', err.message);
process.exit(1);
}
);