Skip to content

Commit d72935c

Browse files
Merge pull request #503 from PermanentOrg/PER-9943-cant-delete-sms-mfa
PER-9943-cant-delete-sms-mfa
2 parents e2f6551 + 241e1fb commit d72935c

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

src/app/core/components/two-factor-auth/two-factor-auth.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
*ngIf="form.get('contactInfo').errors?.maxlength"
8181
class="text-danger"
8282
>
83-
The number exceeds the maximum length of 10 characters.
83+
The number exceeds the maximum length of 11 characters.
8484
</small>
8585
</div>
8686
<div class="send-code-button button-container">

src/app/core/components/two-factor-auth/two-factor-auth.component.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,31 @@ describe('TwoFactorAuthComponent', () => {
6161
instance.method = 'sms';
6262
instance.formatPhoneNumber('1234567890');
6363

64-
expect(instance.form.get('contactInfo').value).toBe('(123) 456 - 7890');
64+
expect(instance.form.get('contactInfo').value).toBe('(123) 456-7890');
65+
});
66+
67+
it('should format phone number with country code correctly', async () => {
68+
const { instance } = await shallow.render();
69+
instance.method = 'sms';
70+
instance.formatPhoneNumber('+12345678900');
71+
72+
expect(instance.form.get('contactInfo').value).toBe('+1 (234) 567-8900');
73+
});
74+
75+
it('should format international phone numbers correctly', async () => {
76+
const { instance } = await shallow.render();
77+
instance.method = 'sms';
78+
instance.formatPhoneNumber('0040123456789');
79+
80+
expect(instance.form.get('contactInfo').value).toBe('+401 (234) 567-89');
81+
});
82+
83+
it('should handle international phone numbers with country code correctly', async () => {
84+
const { instance } = await shallow.render();
85+
instance.method = 'sms';
86+
instance.formatPhoneNumber('+40123456789');
87+
88+
expect(instance.form.get('contactInfo').value).toBe('+401 (234) 567-89');
6589
});
6690

6791
it('should set codeSent to true when sendCode is called', async () => {

src/app/core/components/two-factor-auth/two-factor-auth.component.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import {
33
UntypedFormBuilder,
4+
UntypedFormControl,
45
UntypedFormGroup,
56
Validators,
67
} from '@angular/forms';
@@ -50,16 +51,38 @@ export class TwoFactorAuthComponent implements OnInit {
5051
async ngOnInit() {
5152
this.loading = true;
5253
this.methods = await this.api.idpuser.getTwoFactorMethods();
54+
5355
this.loading = false;
5456
}
5557

5658
formatPhoneNumber(value: string) {
5759
let numbers = value.replace(/\D/g, '');
58-
let char = { 0: '(', 3: ') ', 6: ' - ' };
59-
let formatted = '';
60+
let countryCode = '';
61+
62+
if (numbers.startsWith('00')) {
63+
const match = numbers.match(/^00(\d{1,3})/);
64+
if (match) {
65+
countryCode = `+${match[1]}`;
66+
numbers = numbers.substring(match[0].length);
67+
}
68+
} else if (numbers.startsWith('1') && value.startsWith('+')) {
69+
countryCode = '+1';
70+
numbers = numbers.substring(1);
71+
} else if (value.startsWith('+')) {
72+
const match = numbers.match(/^(\d{1,3})/);
73+
if (match) {
74+
countryCode = `+${match[1]}`;
75+
numbers = numbers.substring(match[1].length);
76+
}
77+
}
78+
79+
const char = { 0: '(', 3: ') ', 6: '-' };
80+
let formatted = countryCode ? `${countryCode} ` : '';
6081
for (let i = 0; i < numbers.length; i++) {
6182
formatted += (char[i] || '') + numbers[i];
6283
}
84+
85+
// Update the form control without emitting events
6386
this.form.get('contactInfo').setValue(formatted, { emitEvent: false });
6487
}
6588

@@ -119,13 +142,22 @@ export class TwoFactorAuthComponent implements OnInit {
119142
} else if (this.method === 'sms') {
120143
contactInfoControl.setValidators([
121144
Validators.required,
122-
Validators.maxLength(17),
123-
Validators.minLength(17),
145+
this.smsLengthValidator(),
124146
]);
125147
}
126148
contactInfoControl.updateValueAndValidity();
127149
}
128150

151+
private smsLengthValidator() {
152+
return (control: UntypedFormControl) => {
153+
const value = control.value || '';
154+
if (value.length === 14 || value.length === 18 || value.length === 17) {
155+
return null;
156+
}
157+
return { invalidSmsLength: true };
158+
};
159+
}
160+
129161
hasMethod(method: string): boolean {
130162
return this.methods.some((m) => m.method === method);
131163
}

0 commit comments

Comments
 (0)