/** * ArkHost Security Pack - 2FA Profile Scripts * * Handles 2FA setup, verification, disable, and backup code regeneration * on the user profile page. Dynamic values are passed via arksp2fa object * (wp_localize_script). */ /* global jQuery, ajaxurl, arksp2fa, qrcode */ jQuery(document).ready(function($) { $('#arksp-setup-2fa').on('click', function() { $('#arksp-2fa-setup').show(); $(this).hide(); // Generate new secret. $.post(ajaxurl, { action: 'arksp_generate_2fa_secret', user_id: arksp2fa.userId, _ajax_nonce: arksp2fa.nonce }, function(response) { if (response.success) { $('#arksp-2fa-secret').text(response.data.secret); // Generate QR code client-side. var qr = qrcode(0, 'M'); qr.addData(response.data.otpauth); qr.make(); $('#arksp-2fa-qr').html(qr.createSvgTag(5, 0)); } }); }); $('#arksp-verify-2fa-setup').on('click', function() { var code = $('#arksp-2fa-verify-code').val(); $.post(ajaxurl, { action: 'arksp_verify_2fa_setup', user_id: arksp2fa.userId, code: code, _ajax_nonce: arksp2fa.nonce }, function(response) { if (response.success && response.data.backup_codes) { // Show backup codes - user MUST save these. var codesHtml = '
'; codesHtml += '

' + arksp2fa.strings.twoFaEnabledTitle + '

'; codesHtml += '

' + arksp2fa.strings.codesNotShownAgain + '

'; codesHtml += '
' + response.data.backup_codes.join('\n') + '
'; codesHtml += '

' + arksp2fa.strings.storeCodesInfo + '

'; codesHtml += ''; codesHtml += '
'; $('#arksp-2fa-setup').html(codesHtml); } else if (response.success) { location.reload(); } else { $('#arksp-2fa-setup-result').html('

' + response.data.message + '

'); } }); }); $('#arksp-disable-2fa').on('click', function() { if (confirm(arksp2fa.strings.confirmDisable)) { $.post(ajaxurl, { action: 'arksp_disable_2fa', user_id: arksp2fa.userId, _ajax_nonce: arksp2fa.nonce }, function(response) { if (response.success) { location.reload(); } }); } }); $('#arksp-regenerate-backup-codes').on('click', function() { if (confirm(arksp2fa.strings.confirmRegenerate)) { $.post(ajaxurl, { action: 'arksp_regenerate_backup_codes', user_id: arksp2fa.userId, _ajax_nonce: arksp2fa.nonce }, function(response) { if (response.success) { $('#arksp-new-backup-codes').text(response.data.codes.join('\n')); $('#arksp-backup-codes-display').show(); } }); } }); });