diff --git a/docs/history.md b/docs/history.md index 34d1da533e..2f5d67310b 100644 --- a/docs/history.md +++ b/docs/history.md @@ -7,6 +7,14 @@ * `email`: - `Email.send` is no longer available. Use `Email.sendAsync` instead. +* `accounts-2fa`: + - Some methods are now async. See below: + - `Accounts._is2faEnabledForUser` + - `(Meteor Method) - generate2faActivationQrCode` + - `(Meteor Method) - enableUser2fa` + - `(Meteor Method) - disableUser2fa` + - `(Meteor Method) - has2faEnabled` + * `accounts-password`: - `Accounts.sendResetPasswordEmail` is now async - `Accounts.sendEnrollmentEmail` is now async diff --git a/packages/accounts-2fa/2fa-server.js b/packages/accounts-2fa/2fa-server.js index 111a655999..45f701a2ee 100644 --- a/packages/accounts-2fa/2fa-server.js +++ b/packages/accounts-2fa/2fa-server.js @@ -13,8 +13,8 @@ Accounts._check2faEnabled = user => { ); }; -Accounts._is2faEnabledForUser = () => { - const user = Meteor.user(); +Accounts._is2faEnabledForUser = async () => { + const user = await Meteor.user(); if (!user) { throw new Meteor.Error('no-logged-user', 'No user logged in.'); } @@ -34,9 +34,9 @@ Accounts._isTokenValid = (secret, code) => { }; Meteor.methods({ - generate2faActivationQrCode(appName) { + async generate2faActivationQrCode(appName) { check(appName, String); - const user = Meteor.user(); + const user = await Meteor.user(); if (!user) { throw new Meteor.Error( @@ -59,7 +59,7 @@ Meteor.methods({ }); const svg = new QRCode(uri).svg(); - Meteor.users.update( + await Meteor.users.update( { _id: user._id }, { $set: { @@ -72,9 +72,9 @@ Meteor.methods({ return { svg, secret, uri }; }, - enableUser2fa(code) { + async enableUser2fa(code) { check(code, String); - const user = Meteor.user(); + const user = await Meteor.user(); if (!user) { throw new Meteor.Error(400, 'No user logged in.'); @@ -94,7 +94,7 @@ Meteor.methods({ Accounts._handleError('Invalid 2FA code', true, 'invalid-2fa-code'); } - Meteor.users.update( + await Meteor.users.update( { _id: user._id }, { $set: { @@ -106,14 +106,14 @@ Meteor.methods({ } ); }, - disableUser2fa() { + async disableUser2fa() { const userId = Meteor.userId(); if (!userId) { throw new Meteor.Error(400, 'No user logged in.'); } - Meteor.users.update( + await Meteor.users.update( { _id: userId }, { $unset: { @@ -122,8 +122,8 @@ Meteor.methods({ } ); }, - has2faEnabled() { - return Accounts._is2faEnabledForUser(); + async has2faEnabled() { + return await Accounts._is2faEnabledForUser(); }, }); diff --git a/packages/accounts-2fa/server_tests.js b/packages/accounts-2fa/server_tests.js index 8a95fc5602..2b627ff595 100644 --- a/packages/accounts-2fa/server_tests.js +++ b/packages/accounts-2fa/server_tests.js @@ -1,15 +1,16 @@ import { Accounts } from 'meteor/accounts-base'; import { Random } from 'meteor/random'; -const findUserById = id => Meteor.users.findOne(id); +const findUserById = + async id => await Meteor.users.findOne(id); -Tinytest.add('account - 2fa - has2faEnabled - server', test => { +Tinytest.addAsync('account - 2fa - has2faEnabled - server', async test => { // Create users - const userWithout2FA = Accounts.insertUserDoc( + const userWithout2FA = await Accounts.insertUserDoc( {}, { emails: [{ address: `${Random.id()}@meteorapp.com`, verified: true }] } ); - const userWith2FA = Accounts.insertUserDoc( + const userWith2FA = await Accounts.insertUserDoc( {}, { emails: [{ address: `${Random.id()}@meteorapp.com`, verified: true }], @@ -19,10 +20,10 @@ Tinytest.add('account - 2fa - has2faEnabled - server', test => { } ); - test.equal(Accounts._check2faEnabled(findUserById(userWithout2FA)), false); - test.equal(Accounts._check2faEnabled(findUserById(userWith2FA)), true); + test.equal(Accounts._check2faEnabled(await findUserById(userWithout2FA)), false); + test.equal(Accounts._check2faEnabled(await findUserById(userWith2FA)), true); // cleanup - Accounts.users.remove(userWithout2FA); - Accounts.users.remove(userWith2FA); + await Accounts.users.remove(userWithout2FA); + await Accounts.users.remove(userWith2FA); });