All files / app/web-ui/scripts request-mfa-scripts.ts

100% Statements 50/50
100% Branches 1/1
100% Functions 1/1
100% Lines 50/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 501x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x
/**
 * This script exposes the requestMfaWithMethod(method: string) function, which will send an API request to resend the MFA code
 */
export const requestMfaScript = (basePath: string) => `
// Track expanded state
const expandedStates = {
    sms: false,
    voice: false
};
 
function togglePhoneOptions(method) {
    const optionsDiv = document.getElementById(method + '-options');
    const button = document.getElementById(method + '-button');
    
    // If already expanded, send request with default number
    if (expandedStates[method]) {
        // Close the options
        button.classList.remove('expanded');
        optionsDiv.classList.remove('show');
        expandedStates[method] = false;
    } else {
        // Expand the options
        optionsDiv.classList.add('show');
        button.classList.add('expanded');
        expandedStates[method] = true;
    }
}
 
async function requestMfaWithMethod(method, id) {
    try {
        document.querySelectorAll('.request-buttons').forEach((el) => {
            el.disabled = true
            el.style['background-color'] = 'rgb(147 157 179)'
        });
 
        const url = id ?
            '${basePath}/api/resend_mfa?method=' + method + '&phoneNumberId=' + id :
            '${basePath}/api/resend_mfa?method=' + method
 
        const response = await fetch(url, {method: 'POST'});
        if (!response.ok) {
            throw new Error("MFA resend request failed: " + response.statusText);
        }
        navigate('${basePath}/submit-mfa');
    } catch (e) {
        alert('Failed to request MFA code: ' + e.message);
    }
    navigate('${basePath}/state');
}
`