|
| 1 | +<!-- |
| 2 | +
|
| 3 | + Copyright © 2025-2026 Ping Identity Corporation. All right reserved. |
| 4 | +
|
| 5 | + This software may be modified and distributed under the terms |
| 6 | + of the MIT license. See the LICENSE file for details. |
| 7 | +
|
| 8 | + --> |
| 9 | + |
| 10 | +<script lang="ts"> |
| 11 | + import { callbackType } from '@forgerock/journey-client'; |
| 12 | + import { afterUpdate } from 'svelte'; |
| 13 | +
|
| 14 | + import T from '$components/_utilities/locale-strings.svelte'; |
| 15 | + import ShieldIcon from '$components/icons/shield-icon.svelte'; |
| 16 | + // Import primitives |
| 17 | + import Alert from '$components/primitives/alert/alert.svelte'; |
| 18 | + import Button from '$components/primitives/button/button.svelte'; |
| 19 | + import Form from '$components/primitives/form/form.svelte'; |
| 20 | + import Text from '$components/primitives/text/text.svelte'; |
| 21 | + // i18n |
| 22 | + import { interpolate } from '$core/_utilities/i18n.utilities'; |
| 23 | + import { convertStringToKey } from '$journey/stages/_utilities/step.utilities'; |
| 24 | +
|
| 25 | + import type { |
| 26 | + ConfirmationCallback, |
| 27 | + HiddenValueCallback, |
| 28 | + JourneyStep, |
| 29 | + } from '@forgerock/journey-client/types'; |
| 30 | +
|
| 31 | + // Types |
| 32 | + import type { StageFormObject, StageJourneyObject } from '$journey/journey.interfaces'; |
| 33 | +
|
| 34 | + export let componentStyle: 'app' | 'inline' | 'modal'; |
| 35 | + export let form: StageFormObject; |
| 36 | + export let formEl: HTMLFormElement | null = null; |
| 37 | + export let journey: StageJourneyObject; |
| 38 | + export let step: JourneyStep; |
| 39 | +
|
| 40 | + type SubStage = 'enrollment' | 'getApp' | 'appLinks'; |
| 41 | +
|
| 42 | + const formFailureMessageId = 'mfaEnrollmentFailureMessage'; |
| 43 | + const formHeaderId = 'mfaEnrollmentHeader'; |
| 44 | + const formElementId = 'mfaEnrollmentForm'; |
| 45 | +
|
| 46 | + let alertNeedsFocus = false; |
| 47 | + let formMessageKey = ''; |
| 48 | + let formAriaDescriptor = formHeaderId; |
| 49 | + let formNeedsFocus = false; |
| 50 | + let subStage: SubStage = 'enrollment'; |
| 51 | + let confirmationCb: ConfirmationCallback | null = null; |
| 52 | + let hiddenValueCb: HiddenValueCallback | null = null; |
| 53 | +
|
| 54 | + afterUpdate(() => { |
| 55 | + if (form?.message) { |
| 56 | + formAriaDescriptor = formFailureMessageId; |
| 57 | + alertNeedsFocus = true; |
| 58 | + formNeedsFocus = false; |
| 59 | + } else { |
| 60 | + formAriaDescriptor = formHeaderId; |
| 61 | + alertNeedsFocus = false; |
| 62 | + formNeedsFocus = true; |
| 63 | + } |
| 64 | + }); |
| 65 | +
|
| 66 | + $: { |
| 67 | + formMessageKey = convertStringToKey(form?.message); |
| 68 | +
|
| 69 | + const confirmationCbs = step.getCallbacksOfType( |
| 70 | + callbackType.ConfirmationCallback, |
| 71 | + ) as ConfirmationCallback[]; |
| 72 | + confirmationCb = confirmationCbs[0] ?? null; |
| 73 | +
|
| 74 | + const hiddenCbs = step.getCallbacksOfType( |
| 75 | + callbackType.HiddenValueCallback, |
| 76 | + ) as HiddenValueCallback[]; |
| 77 | + hiddenValueCb = hiddenCbs[0] ?? null; |
| 78 | +
|
| 79 | + const hiddenId = (hiddenValueCb?.getOutputByName('id', '') as string) ?? ''; |
| 80 | +
|
| 81 | + if (hiddenId.startsWith('getapp-')) { |
| 82 | + subStage = 'getApp'; |
| 83 | + } else if (hiddenId.startsWith('skip-')) { |
| 84 | + subStage = 'enrollment'; |
| 85 | + } else { |
| 86 | + subStage = 'appLinks'; |
| 87 | + } |
| 88 | + } |
| 89 | +
|
| 90 | + function submitWithValue(value: string) { |
| 91 | + confirmationCb?.setInputValue(value); |
| 92 | + form?.submit(); |
| 93 | + } |
| 94 | +
|
| 95 | + function submitHidden(value: string) { |
| 96 | + hiddenValueCb?.setInputValue(value); |
| 97 | + form?.submit(); |
| 98 | + } |
| 99 | +</script> |
| 100 | + |
| 101 | +<Form |
| 102 | + bind:formEl |
| 103 | + ariaDescribedBy={formAriaDescriptor} |
| 104 | + id={formElementId} |
| 105 | + needsFocus={formNeedsFocus} |
| 106 | + onSubmitWhenValid={() => form?.submit()} |
| 107 | +> |
| 108 | + {#if form?.icon && componentStyle !== 'inline'} |
| 109 | + <div class="tw_flex tw_justify-center"> |
| 110 | + <ShieldIcon classes="tw_text-gray-400 tw_fill-current" size="72px" /> |
| 111 | + </div> |
| 112 | + {/if} |
| 113 | + |
| 114 | + {#if form?.message} |
| 115 | + <Alert id={formFailureMessageId} needsFocus={alertNeedsFocus} type="error"> |
| 116 | + {interpolate(formMessageKey, null, form?.message)} |
| 117 | + </Alert> |
| 118 | + {/if} |
| 119 | + |
| 120 | + {#if subStage === 'enrollment'} |
| 121 | + <header id={formHeaderId}> |
| 122 | + <h1 class="tw_primary-header dark:tw_primary-header_dark"> |
| 123 | + <T key="setupTwoStepVerification" /> |
| 124 | + </h1> |
| 125 | + <Text classes="tw_text-center tw_-mt-5 tw_mb-2 tw_py-4"> |
| 126 | + <T key="setupTwoStepVerificationDescription" /> |
| 127 | + </Text> |
| 128 | + </header> |
| 129 | + |
| 130 | + <Alert id="mfaWarning" needsFocus={false} type="warning"> |
| 131 | + <T html={true} key="setupTwoStepVerificationWarning" /> |
| 132 | + </Alert> |
| 133 | + |
| 134 | + <Button |
| 135 | + busy={journey?.loading} |
| 136 | + style="primary" |
| 137 | + type="button" |
| 138 | + width="full" |
| 139 | + onClick={() => submitWithValue('0')} |
| 140 | + > |
| 141 | + <T key="setupTwoStepVerificationButton" /> |
| 142 | + </Button> |
| 143 | + |
| 144 | + <p class="tw_my-4 tw_text-center tw_text-sm"> |
| 145 | + <button |
| 146 | + class="tw_text-link-dark dark:tw_text-link-light tw_underline" |
| 147 | + type="button" |
| 148 | + on:click={() => submitHidden('Skip')} |
| 149 | + > |
| 150 | + <T key="skipForNow" /> |
| 151 | + </button> |
| 152 | + </p> |
| 153 | + {:else if subStage === 'getApp'} |
| 154 | + <header id={formHeaderId}> |
| 155 | + <h1 class="tw_primary-header dark:tw_primary-header_dark"> |
| 156 | + <T key="getAuthenticatorApp" /> |
| 157 | + </h1> |
| 158 | + <Text classes="tw_text-center tw_-mt-5 tw_mb-2 tw_py-4"> |
| 159 | + <T key="getAuthenticatorAppDescription" /> |
| 160 | + </Text> |
| 161 | + </header> |
| 162 | + |
| 163 | + <Button |
| 164 | + busy={journey?.loading} |
| 165 | + style="primary" |
| 166 | + type="button" |
| 167 | + width="full" |
| 168 | + onClick={() => submitWithValue('0')} |
| 169 | + > |
| 170 | + <T key="next" /> |
| 171 | + </Button> |
| 172 | + |
| 173 | + <p class="tw_my-4 tw_text-center tw_text-sm"> |
| 174 | + <button |
| 175 | + class="tw_text-link-dark dark:tw_text-link-light tw_underline" |
| 176 | + type="button" |
| 177 | + on:click={() => submitHidden('Get app')} |
| 178 | + > |
| 179 | + <T key="downloadTheApp" /> |
| 180 | + </button> |
| 181 | + </p> |
| 182 | + {:else} |
| 183 | + <header id={formHeaderId}> |
| 184 | + <h1 class="tw_primary-header dark:tw_primary-header_dark"> |
| 185 | + <T key="getAuthenticatorApp" /> |
| 186 | + </h1> |
| 187 | + <Text classes="tw_text-center tw_-mt-5 tw_mb-2 tw_py-4"> |
| 188 | + <T html={true} key="getAuthenticatorAppLinks" /> |
| 189 | + </Text> |
| 190 | + </header> |
| 191 | + |
| 192 | + <Button |
| 193 | + busy={journey?.loading} |
| 194 | + style="primary" |
| 195 | + type="button" |
| 196 | + width="full" |
| 197 | + onClick={() => submitWithValue('0')} |
| 198 | + > |
| 199 | + <T key="continueButton" /> |
| 200 | + </Button> |
| 201 | + {/if} |
| 202 | +</Form> |
0 commit comments