diff --git a/.changeset/five-wolves-destroy.md b/.changeset/five-wolves-destroy.md new file mode 100644 index 0000000000000..051b77c13b710 --- /dev/null +++ b/.changeset/five-wolves-destroy.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/meteor': patch +--- + +Fixes a UI issue where enabling/disabling TOTP two factor authentication didn't update in real-time. diff --git a/apps/meteor/app/2fa/server/methods/disable.ts b/apps/meteor/app/2fa/server/methods/disable.ts index d2c71febdc357..9e64afb48f20e 100644 --- a/apps/meteor/app/2fa/server/methods/disable.ts +++ b/apps/meteor/app/2fa/server/methods/disable.ts @@ -2,6 +2,7 @@ import type { ServerMethods } from '@rocket.chat/ddp-client'; import { Users } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; +import { notifyOnUserChange } from '../../../lib/server/lib/notifyListener'; import { TOTP } from '../lib/totp'; declare module '@rocket.chat/ddp-client' { @@ -26,7 +27,7 @@ Meteor.methods({ }); } - if (!user.services?.totp) { + if (!user.services?.totp?.enabled) { return false; } @@ -41,6 +42,14 @@ Meteor.methods({ return false; } - return (await Users.disable2FAByUserId(userId)).modifiedCount > 0; + const { modifiedCount } = await Users.disable2FAByUserId(userId); + + if (!modifiedCount) { + return false; + } + + void notifyOnUserChange({ clientAction: 'updated', id: user._id, diff: { 'services.totp.enabled': false } }); + + return true; }, }); diff --git a/apps/meteor/app/2fa/server/methods/validateTempToken.ts b/apps/meteor/app/2fa/server/methods/validateTempToken.ts index 840fadc8cbf7c..89338acd9730f 100644 --- a/apps/meteor/app/2fa/server/methods/validateTempToken.ts +++ b/apps/meteor/app/2fa/server/methods/validateTempToken.ts @@ -2,7 +2,7 @@ import type { ServerMethods } from '@rocket.chat/ddp-client'; import { Users } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; -import { notifyOnUserChangeAsync } from '../../../lib/server/lib/notifyListener'; +import { notifyOnUserChange, notifyOnUserChangeAsync } from '../../../lib/server/lib/notifyListener'; import { TOTP } from '../lib/totp'; declare module '@rocket.chat/ddp-client' { @@ -56,13 +56,18 @@ Meteor.methods({ if (!this.userId) { return; } - const userTokens = await Users.findOneById(this.userId, { projection: { 'services.resume.loginTokens': 1 } }); + const user = await Users.findOneById(this.userId, { projection: { 'services.resume.loginTokens': 1, 'services.totp': 1 } }); return { clientAction: 'updated', id: this.userId, - diff: { 'services.resume.loginTokens': userTokens?.services?.resume?.loginTokens }, + diff: { + 'services.resume.loginTokens': user?.services?.resume?.loginTokens, + ...(user?.services?.totp && { 'services.totp.enabled': user.services.totp.enabled }), + }, }; }); + } else { + void notifyOnUserChange({ clientAction: 'updated', id: user._id, diff: { 'services.totp.enabled': true } }); } } diff --git a/apps/meteor/client/views/account/security/TwoFactorTOTP.tsx b/apps/meteor/client/views/account/security/TwoFactorTOTP.tsx index cc282c2ba64eb..c447fb2449284 100644 --- a/apps/meteor/client/views/account/security/TwoFactorTOTP.tsx +++ b/apps/meteor/client/views/account/security/TwoFactorTOTP.tsx @@ -92,12 +92,13 @@ const TwoFactorTOTP = (props: TwoFactorTOTPProps): ReactElement => { return dispatchToastMessage({ type: 'error', message: t('Invalid_two_factor_code') }); } + setRegisteringTotp(false); setModal(); } catch (error) { dispatchToastMessage({ type: 'error', message: error }); } }, - [closeModal, dispatchToastMessage, setModal, t, verifyCodeFn], + [closeModal, dispatchToastMessage, setModal, t, verifyCodeFn, setRegisteringTotp], ); const handleRegenerateCodes = useCallback(() => {