Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fine-olives-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Fixes the two-factor email "Resend" toast showing `[object Object]` when the server refuses the request, for example after too many resends in one minute. The REST rate limit message also no longer starts with an extra "Error,".
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import userEvent from '@testing-library/user-event';

import TwoFactorEmailModal from './TwoFactorEmailModal';

jest.mock('../../../lib/i18n', () => ({
t: (key: string) => key,
}));

it.each(['error-challenge-expired', 'error-challenge-not-found'])(
'tells the user to log in again when resending with a challenge that is gone (%s)',
async (errorType) => {
Expand Down Expand Up @@ -38,3 +42,25 @@ it('tells the user a new code is sent at most once a minute when resending succe

await waitFor(() => expect(dispatch).toHaveBeenCalledWith({ type: 'success', message: 'One code each minute' }));
});

it('shows the server reason when resending is rate limited', async () => {
const dispatch = jest.fn();
const reason =
'Too many requests. Please slow down. You must wait 42 seconds before trying this endpoint again. [error-too-many-requests]';
const wrapper = mockAppRoot()
.withTranslations('en', 'core', {
'error-email-send-failed': 'Error trying to send email: {{message}}',
'Cloud_resend_email': 'Resend email',
})
.withEndpoint('POST', '/v1/users.2fa.sendEmailCode', () => {
throw Object.assign(new Error(), { success: false, error: reason });
})
.withToastMessageDispatch(dispatch)
.build();

render(<TwoFactorEmailModal emailOrUsername='user' onConfirm={jest.fn()} onClose={jest.fn()} />, { wrapper });

await userEvent.click(screen.getByRole('button', { name: 'Resend email' }));

await waitFor(() => expect(dispatch).toHaveBeenCalledWith({ type: 'error', message: `Error trying to send email: ${reason}` }));
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useTranslation } from 'react-i18next';

import type { OnConfirm } from './TwoFactorModal';
import { Method } from './TwoFactorModal';
import { getErrorMessage } from '../../lib/errorHandling';

export type TwoFactorEmailModalProps = {
onConfirm: OnConfirm;
Expand Down Expand Up @@ -62,7 +63,9 @@ const TwoFactorEmailModal = ({ onConfirm, onClose, invalidAttempt, emailOrUserna
const expired = errorType === 'error-challenge-expired' || errorType === 'error-challenge-not-found';
dispatchToastMessage({
type: 'error',
message: expired ? t('Two-factor_authentication_expired') : t('error-email-send-failed', { message: error }),
message: expired
? t('Two-factor_authentication_expired')
: t('error-email-send-failed', { message: getErrorMessage(error, 'Something_went_wrong') }),
Comment thread
ricardogarim marked this conversation as resolved.
Resolved
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/api/ApiClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ export class APIClass<TBasePath extends string = '', TOperations extends Record<
if (!attemptResult.allowed) {
throw new ClientSafeError(
'error-too-many-requests',
`Error, too many requests. Please slow down. You must wait ${timeToResetAttempsInSeconds} seconds before trying this endpoint again.`,
`Too many requests. Please slow down. You must wait ${timeToResetAttempsInSeconds} seconds before trying this endpoint again.`,
{
timeToReset: attemptResult.timeToReset,
seconds: timeToResetAttempsInSeconds,
Expand Down
Loading