Skip to content

Commit 76fd027

Browse files
committed
PD-4790 initial commit
1 parent fc0699e commit 76fd027

11 files changed

Lines changed: 445 additions & 96 deletions
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
@if (!showRecoveryCode) {
2+
<div class="input-container">
3+
<mat-label
4+
class="orc-font-small-print"
5+
id="2fa-input-label"
6+
i18n="@@account.twoPassword"
7+
[ngClass]="{
8+
error: twoFactorCodeWasTouched && parentForm.get(codeControlName).invalid
9+
}"
10+
>
11+
Two-factor authentication
12+
</mat-label>
13+
<mat-form-field
14+
appearance="outline"
15+
[hideRequiredMarker]="true"
16+
class="no-hint"
17+
>
18+
<input
19+
id="twoFactorCode"
20+
type="text"
21+
inputmode="numeric"
22+
aria-labelledby="2fa-input-label"
23+
[formControlName]="codeControlName"
24+
matInput
25+
#twoFactorCodeInput
26+
[errorStateMatcher]="errorMatcher"
27+
[ngClass]="{
28+
error:
29+
twoFactorCodeWasTouched && parentForm.get(codeControlName).invalid
30+
}"
31+
/>
32+
</mat-form-field>
33+
<div class="two-factor-description orc-font-small-print">
34+
<div class="two-factor-description-text">
35+
@if (!parentForm.get(codeControlName).invalid || !twoFactorCodeWasTouched)
36+
{
37+
<mat-hint i18n="@@account.enterTheCode">
38+
Enter the code from your two-factor authentication app
39+
</mat-hint>
40+
}
41+
<mat-error
42+
*ngIf="parentForm.get(codeControlName).hasError('backendInvalid')"
43+
>
44+
<p
45+
i18n="@@ngOrcid.signin.2fa.badVerificationCode"
46+
class="error-message"
47+
>
48+
Invalid authentication code
49+
</p>
50+
</mat-error>
51+
<mat-error
52+
*ngIf="
53+
twoFactorCodeWasTouched &&
54+
parentForm.get(codeControlName).hasError('required')
55+
"
56+
>
57+
<p
58+
i18n="@@ngOrcid.signin.2fa.verificationCodeRequired"
59+
class="error-message"
60+
>
61+
Authentication code is required
62+
</p>
63+
</mat-error>
64+
<mat-error
65+
*ngIf="
66+
twoFactorCodeWasTouched &&
67+
(parentForm.get(codeControlName).hasError('minlength') ||
68+
parentForm.get(codeControlName).hasError('maxlength'))
69+
"
70+
>
71+
<p
72+
i18n="@@ngOrcid.signin.2fa.badVerificationCodeLength"
73+
class="error-message"
74+
>
75+
Invalid authentication code length
76+
</p>
77+
</mat-error>
78+
</div>
79+
<mat-hint
80+
[ngClass]="{
81+
error:
82+
twoFactorCodeWasTouched && parentForm.get(codeControlName).invalid
83+
}"
84+
class="two-factor-description-length"
85+
>
86+
{{ parentForm.get(codeControlName).value?.length || 0 }}/6
87+
</mat-hint>
88+
</div>
89+
</div>
90+
} @else {
91+
<div class="input-container">
92+
<mat-label
93+
class="orc-font-small-print"
94+
id="recovery-input-label"
95+
i18n="@@ngOrcid.signin.2fa.recoveryCode"
96+
[ngClass]="{
97+
error:
98+
twoFactorRecoveryCodeWasTouched &&
99+
parentForm.get(recoveryControlName).invalid
100+
}"
101+
>
102+
Recovery code
103+
</mat-label>
104+
<mat-form-field
105+
appearance="outline"
106+
[hideRequiredMarker]="true"
107+
class="no-hint"
108+
>
109+
<input
110+
id="twoFactorRecoveryCode"
111+
type="text"
112+
inputmode="numeric"
113+
aria-labelledby="recovery-input-label"
114+
[formControlName]="recoveryControlName"
115+
#twoFactorRecoveryCodeInput
116+
matInput
117+
[errorStateMatcher]="errorMatcher"
118+
[ngClass]="{
119+
error:
120+
twoFactorRecoveryCodeWasTouched &&
121+
parentForm.get(recoveryControlName).invalid
122+
}"
123+
/>
124+
</mat-form-field>
125+
<div class="two-factor-description orc-font-small-print">
126+
<div class="two-factor-description-text">
127+
@if (!parentForm.get(recoveryControlName).invalid ||
128+
!twoFactorRecoveryCodeWasTouched) {
129+
<mat-hint i18n="@@ngOrcid.signin.2fa.noDevice2"
130+
>Enter a recovery code</mat-hint
131+
>
132+
}
133+
<mat-error
134+
*ngIf="parentForm.get(recoveryControlName).hasError('backendInvalid')"
135+
>
136+
<p i18n="@@ngOrcid.signin.2fa.badRecoveryCode" class="error-message">
137+
Invalid recovery code
138+
</p>
139+
</mat-error>
140+
<mat-error
141+
*ngIf="
142+
twoFactorRecoveryCodeWasTouched &&
143+
parentForm.get(recoveryControlName).hasError('required')
144+
"
145+
>
146+
<p
147+
i18n="@@ngOrcid.signin.2fa.recoveryCodeRequired"
148+
class="error-message"
149+
>
150+
Recovery code is required
151+
</p>
152+
</mat-error>
153+
<mat-error
154+
*ngIf="
155+
twoFactorRecoveryCodeWasTouched &&
156+
(parentForm.get(recoveryControlName).hasError('minlength') ||
157+
parentForm.get(recoveryControlName).hasError('maxlength'))
158+
"
159+
>
160+
<p
161+
i18n="@@ngOrcid.signin.2fa.badRecoveryCodeLength"
162+
class="error-message"
163+
>
164+
Invalid recovery code length
165+
</p>
166+
</mat-error>
167+
</div>
168+
<mat-hint
169+
[ngClass]="{
170+
error:
171+
twoFactorRecoveryCodeWasTouched &&
172+
parentForm.get(recoveryControlName).invalid
173+
}"
174+
class="two-factor-description-length"
175+
>
176+
{{ parentForm.get(recoveryControlName).value?.length || 0 }}/10
177+
</mat-hint>
178+
</div>
179+
</div>
180+
}

‎projects/orcid-ui/src/lib/components/two-factor-input/two-factor-input.component.scss‎

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing'
2+
import { TwoFactorInputComponent } from './two-factor-input.component'
3+
4+
describe('TwoFactorInputComponent', () => {
5+
let component: TwoFactorInputComponent
6+
let fixture: ComponentFixture<TwoFactorInputComponent>
7+
8+
beforeEach(async () => {
9+
await TestBed.configureTestingModule({
10+
imports: [TwoFactorInputComponent],
11+
}).compileComponents()
12+
13+
fixture = TestBed.createComponent(TwoFactorInputComponent)
14+
component = fixture.componentInstance
15+
fixture.detectChanges()
16+
})
17+
18+
it('should create', () => {
19+
expect(component).toBeTruthy()
20+
})
21+
})
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { CommonModule } from '@angular/common'
2+
import { Component, Input, OnInit, SkipSelf } from '@angular/core'
3+
import {
4+
AbstractControl,
5+
ControlContainer,
6+
FormGroup,
7+
ReactiveFormsModule,
8+
UntypedFormControl,
9+
} from '@angular/forms'
10+
import { MatFormFieldModule } from '@angular/material/form-field'
11+
import { MatInputModule } from '@angular/material/input'
12+
import { ErrorStateMatcher } from '@angular/material/core'
13+
14+
class ErrorStateMatcherForTwoFactorFields implements ErrorStateMatcher {
15+
constructor() {}
16+
isErrorState(control: UntypedFormControl | AbstractControl | null): boolean {
17+
return !!(control && control.invalid && control.touched && control.dirty)
18+
}
19+
}
20+
21+
@Component({
22+
selector: 'app-two-factor-input',
23+
standalone: true,
24+
imports: [
25+
MatFormFieldModule,
26+
CommonModule,
27+
MatInputModule,
28+
ReactiveFormsModule,
29+
],
30+
viewProviders: [
31+
{
32+
provide: ControlContainer,
33+
useFactory: (container: ControlContainer) => container,
34+
deps: [[new SkipSelf(), ControlContainer]],
35+
},
36+
],
37+
templateUrl: './two-factor-input.component.html',
38+
styleUrl: './two-factor-input.component.scss',
39+
})
40+
export class TwoFactorInputComponent implements OnInit {
41+
@Input() codeControlName = 'twoFactorCode'
42+
@Input() recoveryControlName = 'twoFactorRecoveryCode'
43+
@Input() showRecoveryCode = false
44+
parentForm: FormGroup
45+
errorMatcher = new ErrorStateMatcherForTwoFactorFields()
46+
47+
constructor(private controlContainer: ControlContainer) {}
48+
49+
ngOnInit() {
50+
this.parentForm = this.controlContainer.control as FormGroup
51+
}
52+
53+
get twoFactorCodeWasTouched() {
54+
return (
55+
this.parentForm.get(this.codeControlName).dirty &&
56+
this.parentForm.get(this.codeControlName).touched
57+
)
58+
}
59+
60+
get twoFactorRecoveryCodeWasTouched() {
61+
return (
62+
this.parentForm.get(this.recoveryControlName).dirty &&
63+
this.parentForm.get(this.recoveryControlName).touched
64+
)
65+
}
66+
}

‎projects/orcid-ui/src/public-api.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export * from './lib/components/record-header/record-header.component'
77
export * from './lib/components/text-with-tooltip/text-with-tooltip.component'
88
export * from './lib/directives/accent-button.directive'
99
export * from './lib/components/alert-message/alert-message.component'
10+
export * from './lib/components/two-factor-input/two-factor-input.component'
1011
export * from './lib/components/panel/panel.component'
1112
export * from './lib/components/skeleton-placeholder/skeleton-placeholder.component'

‎src/app/account-settings/account-settings.module.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { SettingsSharingComponent } from './components/settings-sharing/settings
4646
import { SettingsComponent } from './components/settings/settings.component'
4747
import { AccountSettingsComponent } from './pages/account-settings/account-settings.component'
4848
import { MatDialogModule } from '@angular/material/dialog'
49-
import { AlertMessageComponent } from '@orcid/ui'
49+
import { AlertMessageComponent, TwoFactorInputComponent } from '@orcid/ui'
5050
import { ConfirmDeactivateAccountComponent } from './pages/confirm-deactivate-account/confirm-deactivate-account.component'
5151

5252
@NgModule({
@@ -73,7 +73,6 @@ import { ConfirmDeactivateAccountComponent } from './pages/confirm-deactivate-ac
7373
SettingsSharingComponent,
7474
SettingsSharingQrCodeComponent,
7575
SettingsSharingHtmlCodeComponent,
76-
ConfirmDeactivateAccountComponent,
7776
],
7877
imports: [
7978
CommonModule,
@@ -102,6 +101,7 @@ import { ConfirmDeactivateAccountComponent } from './pages/confirm-deactivate-ac
102101
AccountPanelModule,
103102
TopBarMyPublicRecordPreviewModule,
104103
AlertMessageComponent,
104+
TwoFactorInputComponent,
105105
],
106106
providers: [VerificationEmailModalService],
107107
})

0 commit comments

Comments
 (0)