|
| 1 | +<app-documentation-page |
| 2 | + title="Two Factor Authentication Form" |
| 3 | + description="The <app-two-factor-auth-form> component is a subform that validates a user's 2fa or recovery code." |
| 4 | +> |
| 5 | + <div controls> |
| 6 | + <p>Customize the subform to preview different configurations:</p> |
| 7 | + |
| 8 | + <div style="display: grid; gap: 16px; margin-bottom: 24px"> |
| 9 | + <mat-checkbox [(ngModel)]="showHelpText"> |
| 10 | + Display help text / controls |
| 11 | + </mat-checkbox> |
| 12 | + |
| 13 | + <mat-checkbox [(ngModel)]="showAlert"> Display alert </mat-checkbox> |
| 14 | + |
| 15 | + <button |
| 16 | + style="width: 100px; height: 40px; margin-left: 10px" |
| 17 | + mat-button |
| 18 | + (click)="form.reset()" |
| 19 | + > |
| 20 | + Reset form |
| 21 | + </button> |
| 22 | + </div> |
| 23 | + </div> |
| 24 | + |
| 25 | + <div examples> |
| 26 | + <form [formGroup]="form" class="w-[537px]"> |
| 27 | + <app-two-factor-auth-form |
| 28 | + [showAlert]="showAlert" |
| 29 | + [showHelpText]="showHelpText" |
| 30 | + codeControlName="twoFactorCode" |
| 31 | + recoveryControlName="twoFactorRecoveryCode" |
| 32 | + > |
| 33 | + </app-two-factor-auth-form> |
| 34 | + </form> |
| 35 | + </div> |
| 36 | + |
| 37 | + <div usage style="font-size: 14px"> |
| 38 | + <p> |
| 39 | + This component is a sub-form that must be placed inside a parent |
| 40 | + <code>[formGroup]</code>. It manages the UI switching and validation logic |
| 41 | + between the 2FA code and recovery code inputs. |
| 42 | + </p> |
| 43 | + |
| 44 | + <p>To implement this component, use the code highlighted in yellow.</p> |
| 45 | + |
| 46 | + <h4>1. Template</h4> |
| 47 | + <p> |
| 48 | + Place the component inside your existing form. Ensure the control names |
| 49 | + passed here match your FormGroup definition. |
| 50 | + </p> |
| 51 | + <pre><code class="language-html"><form [formGroup]="form" (ngSubmit)="save()"> |
| 52 | + <!-- Other inputs... --> |
| 53 | + |
| 54 | + @if (twoFactorEnabled) { |
| 55 | + <span style="background-color: #fff59d; color: #000;"><app-two-factor-auth-form</span> |
| 56 | + <span style="background-color: #fff59d; color: #000;">codeControlName="twoFactorCode"</span> |
| 57 | + <span style="background-color: #fff59d; color: #000;">recoveryControlName="twoFactorRecoveryCode"</span> |
| 58 | + <span style="background-color: #fff59d; color: #000;">[showAlert]="true"></span> |
| 59 | + <span style="background-color: #fff59d; color: #000;"></app-two-factor-auth-form></span> |
| 60 | + } |
| 61 | +</form></code></pre> |
| 62 | + |
| 63 | + <h4>2. Form Configuration</h4> |
| 64 | + <p> |
| 65 | + Initialize the controls in your parent component. Note that |
| 66 | + <code>Validators.required</code> is managed automatically by the child |
| 67 | + component and should not be added here. |
| 68 | + </p> |
| 69 | + <pre><code class="language-typescript">this.form = this.fb.group({ |
| 70 | + // ... other controls |
| 71 | + <span style="background-color: #fff59d; color: #000;">twoFactorCode: [null, [Validators.minLength(6), Validators.maxLength(6)]],</span> |
| 72 | + <span style="background-color: #fff59d; color: #000;">twoFactorRecoveryCode: [null, [Validators.minLength(10), Validators.maxLength(10)]],</span> |
| 73 | +});</code></pre> |
| 74 | + |
| 75 | + <h4>3. Handling Submit & Responses</h4> |
| 76 | + <p> |
| 77 | + Use <code>@ViewChild</code> to access the component. This allows you to |
| 78 | + delegate backend error mapping and focus management. |
| 79 | + </p> |
| 80 | + <pre><code class="language-typescript"><span style="background-color: #fff59d; color: #000;">@ViewChild(TwoFactorAuthFormComponent) twoFactorComponent: TwoFactorAuthFormComponent;</span> |
| 81 | + |
| 82 | +save() { |
| 83 | + if (this.form.valid) { |
| 84 | + this.service.update(this.form.value).subscribe(response => { |
| 85 | + // Pass backend response to child to handle errors/focus |
| 86 | + <span style="background-color: #fff59d; color: #000;">this.twoFactorComponent?.processBackendResponse(response);</span> |
| 87 | + <span style="color: #666">// Used when we don't know if the user</span> |
| 88 | + <span style="color: #666">// has 2fa enabled (e.g. signin)</span> |
| 89 | + <span style="background-color: #fff59d; color: #000;">this.twoFactorEnabled = response.twoFactorEnabled;</span> |
| 90 | + }); |
| 91 | + } |
| 92 | +}</code></pre> |
| 93 | + |
| 94 | + <h4>4. Interface Definition</h4> |
| 95 | + <p> |
| 96 | + The endpoint payload/response object needs to extend the |
| 97 | + <strong><code>TwoFactorAuthForm</code></strong> interface/pojo. |
| 98 | + </p> |
| 99 | + <pre><code class="language-typescript">export interface TwoFactorAuthForm { |
| 100 | + invalidTwoFactorCode?: boolean; |
| 101 | + invalidTwoFactorRecoveryCode?: boolean; |
| 102 | + twoFactorCode?: string; |
| 103 | + twoFactorRecoveryCode?: string; |
| 104 | + twoFactorEnabled?: boolean; |
| 105 | +}</code></pre> |
| 106 | + |
| 107 | + <h4>5. Backend Implementation</h4> |
| 108 | + <p> |
| 109 | + The backend should use the |
| 110 | + <code>TwoFactorAuthenticationManager</code> to validate the request. |
| 111 | + </p> |
| 112 | + <p> |
| 113 | + If no codes are provided, the backend will assume it is a two-step form |
| 114 | + flow: it will set the <code>twoFactorEnabled</code> flag to true and |
| 115 | + return the form so the UI can display the inputs. Otherwise, it will |
| 116 | + validate the codes, assign error flags if necessary, and return the form. |
| 117 | + </p> |
| 118 | + <p> |
| 119 | + In case the 2FA check passes successfully, the manager returns |
| 120 | + <code>true</code> and the function proceeds to the next step. |
| 121 | + </p> |
| 122 | + <pre><code class="language-java"><span style="background-color: #fff59d; color: #000;">if (!twoFactorAuthenticationManager.validateTwoFactorAuthForm(orcid, form)) {</span> |
| 123 | + <span style="background-color: #fff59d; color: #000;">return form;</span> |
| 124 | +<span style="background-color: #fff59d; color: #000;">}</span></code></pre> |
| 125 | + </div> |
| 126 | + <div inputs> |
| 127 | + <ul style="font-size: 14px"> |
| 128 | + <li> |
| 129 | + <code style="font-weight: bold">codeControlName</code>: |
| 130 | + <code>string</code>. The name of the form control for the 6-digit |
| 131 | + authentication code. (default <code>'twoFactorCode'</code>) |
| 132 | + </li> |
| 133 | + <li> |
| 134 | + <code style="font-weight: bold">recoveryControlName</code>: |
| 135 | + <code>string</code>. The name of the form control for the 10-character |
| 136 | + recovery code. (default <code>'twoFactorRecoveryCode'</code>) |
| 137 | + </li> |
| 138 | + <li> |
| 139 | + <code style="font-weight: bold">showAlert</code>: <code>boolean</code>. |
| 140 | + Whether to display the notice alert indicating 2FA is active. (default |
| 141 | + <code>false</code>) |
| 142 | + </li> |
| 143 | + <li> |
| 144 | + <code style="font-weight: bold">showHelpText</code>: |
| 145 | + <code>boolean</code>. Whether to display the helper links (e.g. "Use a |
| 146 | + recovery code instead") below the inputs. (default <code>true</code>) |
| 147 | + </li> |
| 148 | + </ul> |
| 149 | + </div> |
| 150 | +</app-documentation-page> |
0 commit comments