|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Ping Identity Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This software may be modified and distributed under the terms |
| 5 | + * of the MIT license. See the LICENSE file for details. |
| 6 | + */ |
| 7 | +import { RecoveryCodes } from '@forgerock/journey-client/recovery-codes'; |
| 8 | +import type { JourneyStep, ConfirmationCallback } from '@forgerock/journey-client/types'; |
| 9 | + |
| 10 | +export function renderRecoveryCodesStep(journeyEl: HTMLDivElement, step: JourneyStep): boolean { |
| 11 | + if (!RecoveryCodes.isDisplayStep(step)) { |
| 12 | + return false; |
| 13 | + } |
| 14 | + |
| 15 | + const codes = RecoveryCodes.getCodes(step); |
| 16 | + const deviceName = RecoveryCodes.getDeviceName(step); |
| 17 | + |
| 18 | + console.log('Recovery Codes step detected via RecoveryCodes module'); |
| 19 | + console.log('Recovery codes:', JSON.stringify(codes)); |
| 20 | + console.log('Device name:', deviceName); |
| 21 | + |
| 22 | + const container = document.createElement('div'); |
| 23 | + container.id = 'recovery-codes-container'; |
| 24 | + |
| 25 | + const header = document.createElement('h3'); |
| 26 | + header.id = 'recovery-codes-header'; |
| 27 | + header.innerText = 'Your Recovery Codes'; |
| 28 | + container.appendChild(header); |
| 29 | + |
| 30 | + const instruction = document.createElement('p'); |
| 31 | + instruction.innerText = |
| 32 | + 'You must make a copy of these recovery codes. They cannot be displayed again.'; |
| 33 | + instruction.style.color = '#666'; |
| 34 | + container.appendChild(instruction); |
| 35 | + |
| 36 | + const codesContainer = document.createElement('div'); |
| 37 | + codesContainer.id = 'recovery-codes-list'; |
| 38 | + codesContainer.style.cssText = ` |
| 39 | + padding: 15px; |
| 40 | + background-color: #f5f5f5; |
| 41 | + border: 1px solid #ddd; |
| 42 | + border-radius: 4px; |
| 43 | + font-family: monospace; |
| 44 | + font-size: 14px; |
| 45 | + margin: 10px 0; |
| 46 | + display: grid; |
| 47 | + grid-template-columns: repeat(2, 1fr); |
| 48 | + gap: 8px; |
| 49 | + `; |
| 50 | + |
| 51 | + codes.forEach((code, index) => { |
| 52 | + const codeEl = document.createElement('div'); |
| 53 | + codeEl.className = 'recovery-code'; |
| 54 | + codeEl.setAttribute('data-code-index', String(index)); |
| 55 | + codeEl.innerText = code; |
| 56 | + codeEl.style.cssText = ` |
| 57 | + padding: 5px 10px; |
| 58 | + background-color: white; |
| 59 | + border: 1px solid #ccc; |
| 60 | + border-radius: 3px; |
| 61 | + text-align: center; |
| 62 | + `; |
| 63 | + codesContainer.appendChild(codeEl); |
| 64 | + }); |
| 65 | + |
| 66 | + container.appendChild(codesContainer); |
| 67 | + |
| 68 | + if (deviceName) { |
| 69 | + const deviceInfo = document.createElement('p'); |
| 70 | + deviceInfo.id = 'recovery-codes-device'; |
| 71 | + deviceInfo.innerText = `Device: ${deviceName}`; |
| 72 | + deviceInfo.style.color = '#666'; |
| 73 | + container.appendChild(deviceInfo); |
| 74 | + } |
| 75 | + |
| 76 | + const confirmationCallbacks = |
| 77 | + step.getCallbacksOfType<ConfirmationCallback>('ConfirmationCallback'); |
| 78 | + |
| 79 | + if (confirmationCallbacks.length > 0) { |
| 80 | + const confirmCb = confirmationCallbacks[0]; |
| 81 | + const options = confirmCb.getOptions(); |
| 82 | + |
| 83 | + const optionsContainer = document.createElement('div'); |
| 84 | + optionsContainer.style.marginTop = '15px'; |
| 85 | + |
| 86 | + options.forEach((option, index) => { |
| 87 | + const label = document.createElement('label'); |
| 88 | + label.style.display = 'block'; |
| 89 | + label.style.marginBottom = '5px'; |
| 90 | + |
| 91 | + const radio = document.createElement('input'); |
| 92 | + radio.type = 'radio'; |
| 93 | + radio.name = 'recovery-confirmation'; |
| 94 | + radio.value = String(index); |
| 95 | + radio.checked = index === confirmCb.getDefaultOption(); |
| 96 | + radio.addEventListener('change', () => { |
| 97 | + confirmCb.setOptionIndex(index); |
| 98 | + }); |
| 99 | + |
| 100 | + if (index === confirmCb.getDefaultOption()) { |
| 101 | + confirmCb.setOptionIndex(index); |
| 102 | + } |
| 103 | + |
| 104 | + label.appendChild(radio); |
| 105 | + label.appendChild(document.createTextNode(` ${option}`)); |
| 106 | + optionsContainer.appendChild(label); |
| 107 | + }); |
| 108 | + |
| 109 | + container.appendChild(optionsContainer); |
| 110 | + } |
| 111 | + |
| 112 | + journeyEl.appendChild(container); |
| 113 | + |
| 114 | + return true; |
| 115 | +} |
0 commit comments