-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
2fa-client.js
76 lines (69 loc) · 2.3 KB
/
2fa-client.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
import { Accounts } from 'meteor/accounts-base';
// Used in the various functions below to handle errors consistently
const reportError = (error, callback) => {
if (callback) {
callback(error);
} else {
throw error;
}
};
/**
* @summary Verify if the logged user has 2FA enabled
* @locus Client
* @param {Function} [callback] Called with a boolean on success that indicates whether the user has
* or not 2FA enabled, or with a single `Error` argument on failure.
*/
Accounts.has2faEnabled = callback => {
Accounts.connection.call('has2faEnabled', callback);
};
/**
* @summary Generates a svg QR code and save secret on user
* @locus Client
* @param {String} appName It's the name of your app that will show up when the user scans the QR code.
* @param {Function} callback
* Called with a single `Error` argument on failure.
* Or, on success, called with an object containing the QR code in SVG format (svg),
* the QR secret (secret), and the URI so the user can manually activate the 2FA without reading the QR code (uri).
*/
Accounts.generate2faActivationQrCode = (appName, callback) => {
if (!appName) {
throw new Meteor.Error(
500,
'An app name is necessary when calling the function generate2faActivationQrCode'
);
}
if (!callback) {
throw new Meteor.Error(
500,
'A callback is necessary when calling the function generate2faActivationQrCode so a QR code can be provided'
);
}
Accounts.connection.call('generate2faActivationQrCode', appName, callback);
};
/**
* @summary Enable the user 2FA
* @locus Client
* @param {String} code Code received from the authenticator app.
* @param {Function} [callback] Optional callback.
* Called with no arguments on success, or with a single `Error` argument
* on failure.
*/
Accounts.enableUser2fa = (code, callback) => {
if (!code) {
return reportError(
new Meteor.Error(400, 'Must provide a code to validate'),
callback
);
}
Accounts.connection.call('enableUser2fa', code, callback);
};
/**
* @summary Disable user 2FA
* @locus Client
* @param {Function} [callback] Optional callback.
* Called with no arguments on success, or with a single `Error` argument
* on failure.
*/
Accounts.disableUser2fa = callback => {
Accounts.connection.call('disableUser2fa', callback);
};