Skip to content

Commit 59e0891

Browse files
committed
registration: Stop lying to users that registration worked when they are already registered
fix #246
1 parent 46d8d35 commit 59e0891

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

backend/src/routes/auth/register.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub async fn register(
171171
.await
172172
{
173173
Ok(Ok(_)) => (),
174-
Ok(Err(Error::UserAlreadyExists)) => return Ok(HttpResponse::Created()),
174+
Ok(Err(Error::UserAlreadyExists)) => return Err(Error::UserAlreadyExists.into()),
175175
Ok(Err(_)) => return Err(Error::InternalError.into()),
176176
Err(_) => return Err(Error::InternalError.into()),
177177
}
@@ -493,7 +493,7 @@ pub(crate) mod tests {
493493
.set_json(user)
494494
.to_request();
495495
let resp = test::call_service(&app, req).await;
496-
assert!(resp.status().is_success());
496+
assert_eq!(resp.status().as_u16(), 409); // Conflict - user already exists
497497

498498
let pool = test_connection_pool();
499499
let mut conn = pool.get().unwrap();

frontend/src/components/Register.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ export class Register extends Component<Record<string, never>, RegisterState> {
200200
})
201201
if (response.status === 201) {
202202
showAlert(i18n.t("register.registration_successful"), "success", "register", i18n.t("alert_default_success"));
203+
} else if (response.status === 409) {
204+
showAlert(i18n.t("register.email_already_exists"), "danger");
205+
return;
203206
} else {
204207
const text = `Failed with status ${response.status}: ${error}`;
205208
showAlert(text, "danger");

frontend/src/components/__tests__/register.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,37 @@ describe('Register Component', () => {
389389
});
390390
});
391391

392+
it('handles email already exists error (409 conflict)', async () => {
393+
mockUtils.fetchClient.POST.mockResolvedValue({
394+
response: { status: 409 },
395+
error: 'An account with this email already exists',
396+
});
397+
398+
render(<Register />);
399+
400+
const nameInput = screen.getByTestId('text-input');
401+
const emailInput = screen.getByTestId('email-input');
402+
const passwordInputs = screen.getAllByTestId('password-input');
403+
const checkboxes = screen.getAllByTestId('checkbox');
404+
405+
fireEvent.change(nameInput, { target: { value: 'John Doe' } });
406+
fireEvent.change(emailInput, { target: { value: 'existing@example.com' } });
407+
fireEvent.change(passwordInputs[0], { target: { value: 'ValidPass123!' } });
408+
fireEvent.change(passwordInputs[1], { target: { value: 'ValidPass123!' } });
409+
fireEvent.click(checkboxes[0]);
410+
fireEvent.click(checkboxes[1]);
411+
412+
const submitButton = screen.getByTestId('submit-button');
413+
fireEvent.click(submitButton);
414+
415+
await waitFor(() => {
416+
expect(mockShowAlert).toHaveBeenCalledWith(
417+
'email_already_exists',
418+
'danger'
419+
);
420+
});
421+
});
422+
392423
it('handles salt generation error', async () => {
393424
mockUtils.get_salt.mockRejectedValue('Salt generation failed');
394425

frontend/src/locales/de.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ export const de ={
165165
"save_recovery_data_text": "Da die Zugangsdaten für die Geräte nur mithilfe des korrekten Passworts entschlüsselt werden können brauchst du, falls du dein Passwort vergessen solltest, diese Datei um den Zugang zu deinen Geräten wiederherzustellen. Bewahre diese Datei sicher und für niemanden sonst zugänglich auf, da sie mit deinem Passwort gleichzustellen ist.",
166166
"save_recovery_data_confirmation": "Ich habe die Wiederherstellungsdatei heruntergeladen und sicher gespeichert",
167167
"close": "Schließen",
168-
"registration_successful": "Die Registrierung war erfolgreich. Du solltest innerhalb der nächsten paar Minuten eine Email mit einem Bestätigungslink erhalten"
168+
"registration_successful": "Die Registrierung war erfolgreich. Du solltest innerhalb der nächsten paar Minuten eine Email mit einem Bestätigungslink erhalten",
169+
"email_already_exists": "Ein Konto mit dieser E-Mail-Adresse existiert bereits."
169170
,"resend_verification": "Bestätigungs-E-Mail erneut senden"
170171
,"resend_success": "Bestätigungs-E-Mail gesendet (falls noch nicht bestätigt)."
171172
,"resend_error": "Bestätigungs-E-Mail konnte nicht gesendet werden. Bitte versuche es später erneut."

frontend/src/locales/en.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ export const en = {
165165
"save_recovery_data_text": "Since the access data for the devices can only be decrypted with the correct password, you need this file to restore access to your devices if you forget your password. Keep this file safe and inaccessible to others, as it is equivalent to your password.",
166166
"save_recovery_data_confirmation": "I have downloaded and safely stored the recovery file",
167167
"close": "Close",
168-
"registration_successful": "Registration was successful. You should receive an email with a confirmation link within the next few minutes."
168+
"registration_successful": "Registration was successful. You should receive an email with a confirmation link within the next few minutes.",
169+
"email_already_exists": "An account with this email address already exists."
169170
,"resend_verification": "Resend verification email"
170171
,"resend_success": "Verification email sent (if the address is not yet verified)."
171172
,"resend_error": "Could not resend verification email. Please try again later."

0 commit comments

Comments
 (0)