| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
document.addEventListener('click', function (event) { |
| 5 |
if (!event.target || event.target.id !== 'twofa-resend') return; |
| 6 |
event.preventDefault(); |
| 7 |
var form = document.getElementById('loginform'); |
| 8 |
if (!form) return; |
| 9 |
var codeField = form.querySelector('input[name="twofa_code"]'); |
| 10 |
if (codeField) codeField.value = ''; |
| 11 |
var input = form.querySelector('input[name="twofa_resend"]'); |
| 12 |
if (!input) { |
| 13 |
input = document.createElement('input'); |
| 14 |
input.type = 'hidden'; |
| 15 |
input.name = 'twofa_resend'; |
| 16 |
form.appendChild(input); |
| 17 |
} |
| 18 |
input.value = '1'; |
| 19 |
|
| 20 |
var previousNoValidate = form.noValidate; |
| 21 |
form.noValidate = true; |
| 22 |
try { |
| 23 |
if (form.requestSubmit) { |
| 24 |
form.requestSubmit(); |
| 25 |
} else { |
| 26 |
var submitEvent = document.createEvent('Event'); |
| 27 |
submitEvent.initEvent('submit', true, true); |
| 28 |
form.dispatchEvent(submitEvent); |
| 29 |
} |
| 30 |
} finally { |
| 31 |
form.noValidate = previousNoValidate; |
| 32 |
} |
| 33 |
}); |
| 34 |
|
| 35 |
document.addEventListener('DOMContentLoaded', function () { |
| 36 |
var loginForm = document.getElementById('loginform'); |
| 37 |
var usernameField = document.getElementById('user_login'); |
| 38 |
var passwordField = document.getElementById('user_pass'); |
| 39 |
var loginButton = document.getElementById('wp-submit'); |
| 40 |
var loginError = document.getElementById('login_error'); |
| 41 |
var isTwoFAEnabled = false; |
| 42 |
var requestInFlight = false; |
| 43 |
var resendTimer = null; |
| 44 |
var progressBar = document.getElementsByClassName('wp2fa-progress-bar')[0]; |
| 45 |
|
| 46 |
if (!loginForm || !usernameField || !passwordField || !loginButton) return; |
| 47 |
loginForm.addEventListener('submit', handleSubmit); |
| 48 |
|
| 49 |
function handleSubmit(event) { |
| 50 |
var formData = new FormData(loginForm); |
| 51 |
var isResendRequest = formData.get('twofa_resend') === '1'; |
| 52 |
|
| 53 |
event.preventDefault(); |
| 54 |
if (requestInFlight) return; |
| 55 |
requestInFlight = true; |
| 56 |
showProgressBar(); |
| 57 |
disableLoginButton(); |
| 58 |
|
| 59 |
if (isResendRequest) formData.delete('twofa_code'); |
| 60 |
clearResendStatus(); |
| 61 |
|
| 62 |
fetch(loginForm.action, { |
| 63 |
method: 'POST', |
| 64 |
body: formData, |
| 65 |
credentials: 'same-origin' |
| 66 |
}).then(function (response) { |
| 67 |
return response.text().then(function (responseText) { |
| 68 |
return { |
| 69 |
text: responseText, |
| 70 |
url: response.url, |
| 71 |
redirected: response.redirected |
| 72 |
}; |
| 73 |
}); |
| 74 |
}).then(function (responseData) { |
| 75 |
try { |
| 76 |
return JSON.parse(responseData.text); |
| 77 |
} catch (error) { |
| 78 |
return { |
| 79 |
success: false, |
| 80 |
html: responseData.text, |
| 81 |
responseUrl: responseData.url, |
| 82 |
redirected: responseData.redirected |
| 83 |
}; |
| 84 |
} |
| 85 |
}).then(function (data) { |
| 86 |
if (data.success && data.data && data.data.twofa_enabled) { |
| 87 |
isTwoFAEnabled = true; |
| 88 |
showTwoFAField(data.data); |
| 89 |
clearLoginError(); |
| 90 |
if (isResendRequest) { |
| 91 |
displayResendStatus(data.data.code_sent |
| 92 |
? 'A new sign-in code was sent.' |
| 93 |
: 'Your previous code is still valid.'); |
| 94 |
} |
| 95 |
return; |
| 96 |
} |
| 97 |
if (data.success) { |
| 98 |
proceedWithLogin(); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
if (data.html) { |
| 103 |
handleHtmlResponse(data.html, data.responseUrl, data.redirected); |
| 104 |
} else if (data.data && data.data.message) { |
| 105 |
displayError(data.data.message); |
| 106 |
} else { |
| 107 |
displayError('An unknown error occurred'); |
| 108 |
} |
| 109 |
if (isTwoFAEnabled) showTwoFAField(); |
| 110 |
if (data.data && data.data.resend_after) restartResendCooldown(data.data.resend_after); |
| 111 |
}).catch(function () { |
| 112 |
displayError('An error occurred while processing your request'); |
| 113 |
if (isTwoFAEnabled) showTwoFAField(); |
| 114 |
}).then(finishRequest, finishRequest); |
| 115 |
} |
| 116 |
|
| 117 |
function finishRequest() { |
| 118 |
var resendInput = loginForm.querySelector('input[name="twofa_resend"]'); |
| 119 |
if (resendInput) resendInput.remove(); |
| 120 |
hideProgressBar(); |
| 121 |
enableLoginButton(); |
| 122 |
requestInFlight = false; |
| 123 |
} |
| 124 |
|
| 125 |
function handleHtmlResponse(html, responseUrl, redirected) { |
| 126 |
var parser = new DOMParser(); |
| 127 |
var doc = parser.parseFromString(html, 'text/html'); |
| 128 |
var errorElement = doc.getElementById('login_error'); |
| 129 |
if (errorElement) { |
| 130 |
displayError(errorElement.textContent.trim()); |
| 131 |
} else if (isTwoFAEnabled && redirected && responseUrl) { |
| 132 |
window.location.assign(responseUrl); |
| 133 |
} else if (isTwoFAEnabled) { |
| 134 |
displayError('An unknown error occurred'); |
| 135 |
} else { |
| 136 |
proceedWithLogin(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
function proceedWithLogin() { |
| 141 |
loginForm.removeEventListener('submit', handleSubmit); |
| 142 |
hideProgressBar(); |
| 143 |
enableLoginButton(); |
| 144 |
requestInFlight = false; |
| 145 |
loginForm.submit(); |
| 146 |
} |
| 147 |
|
| 148 |
function showTwoFAField(data) { |
| 149 |
var twofaField = document.getElementById('twofa-code-field'); |
| 150 |
if (!twofaField) { |
| 151 |
var template = document.getElementById('twofa-field-template'); |
| 152 |
if (!template || !template.content || !template.content.firstElementChild) return; |
| 153 |
twofaField = template.content.firstElementChild.cloneNode(true); |
| 154 |
var passwordContainer = passwordField.closest ? passwordField.closest('.user-pass-wrap') : passwordField.parentNode; |
| 155 |
var insertionPoint = passwordContainer || passwordField.parentNode; |
| 156 |
insertionPoint.parentNode.insertBefore(twofaField, insertionPoint.nextSibling); |
| 157 |
} |
| 158 |
|
| 159 |
var resendButton = twofaField.querySelector('#twofa-resend'); |
| 160 |
var destination = twofaField.querySelector('#twofa-destination'); |
| 161 |
var codeField = twofaField.querySelector('#twofa-code'); |
| 162 |
if (data && data.twofa_method === 'email_otp') { |
| 163 |
if (codeField) { |
| 164 |
codeField.maxLength = 8; |
| 165 |
codeField.minLength = 8; |
| 166 |
} |
| 167 |
if (resendButton) { |
| 168 |
resendButton.style.display = 'inline-block'; |
| 169 |
startResendCooldown(resendButton, Number(data.resend_after) || 0); |
| 170 |
} |
| 171 |
if (destination && data.masked_destination) { |
| 172 |
destination.textContent = 'A sign-in code was sent to ' + data.masked_destination + '.'; |
| 173 |
} |
| 174 |
} |
| 175 |
twofaField.style.display = 'block'; |
| 176 |
if (codeField) codeField.value = ''; |
| 177 |
} |
| 178 |
|
| 179 |
function restartResendCooldown(seconds) { |
| 180 |
var resendButton = document.getElementById('twofa-resend'); |
| 181 |
if (resendButton) startResendCooldown(resendButton, Number(seconds) || 0); |
| 182 |
} |
| 183 |
|
| 184 |
function startResendCooldown(button, seconds) { |
| 185 |
if (resendTimer) window.clearInterval(resendTimer); |
| 186 |
var remaining = Math.max(0, Math.floor(seconds)); |
| 187 |
function render() { |
| 188 |
button.disabled = remaining > 0; |
| 189 |
button.textContent = remaining > 0 ? 'Send a new code (' + remaining + 's)' : 'Send a new code'; |
| 190 |
} |
| 191 |
render(); |
| 192 |
if (remaining < 1) return; |
| 193 |
resendTimer = window.setInterval(function () { |
| 194 |
remaining -= 1; |
| 195 |
render(); |
| 196 |
if (remaining < 1) { |
| 197 |
window.clearInterval(resendTimer); |
| 198 |
resendTimer = null; |
| 199 |
} |
| 200 |
}, 1000); |
| 201 |
} |
| 202 |
|
| 203 |
function clearLoginError() { |
| 204 |
if (loginError) loginError.style.display = 'none'; |
| 205 |
} |
| 206 |
|
| 207 |
function clearResendStatus() { |
| 208 |
var resendStatus = document.getElementById('twofa-resend-status'); |
| 209 |
if (!resendStatus) return; |
| 210 |
resendStatus.textContent = ''; |
| 211 |
resendStatus.style.display = 'none'; |
| 212 |
} |
| 213 |
|
| 214 |
function displayResendStatus(message) { |
| 215 |
var resendStatus = document.getElementById('twofa-resend-status'); |
| 216 |
if (!resendStatus) return; |
| 217 |
resendStatus.textContent = message; |
| 218 |
resendStatus.style.display = 'block'; |
| 219 |
} |
| 220 |
|
| 221 |
function displayError(message) { |
| 222 |
if (!loginError) { |
| 223 |
loginError = document.createElement('div'); |
| 224 |
loginError.id = 'login_error'; |
| 225 |
loginForm.parentNode.insertBefore(loginError, loginForm); |
| 226 |
} |
| 227 |
loginError.textContent = message; |
| 228 |
loginError.style.display = 'block'; |
| 229 |
} |
| 230 |
|
| 231 |
function showProgressBar() { |
| 232 |
if (progressBar) progressBar.classList.add('show'); |
| 233 |
} |
| 234 |
|
| 235 |
function hideProgressBar() { |
| 236 |
if (progressBar) progressBar.classList.remove('show'); |
| 237 |
} |
| 238 |
|
| 239 |
function disableLoginButton() { |
| 240 |
loginButton.disabled = true; |
| 241 |
loginButton.value = 'Verifying...'; |
| 242 |
} |
| 243 |
|
| 244 |
function enableLoginButton() { |
| 245 |
loginButton.disabled = false; |
| 246 |
loginButton.value = 'Log In'; |
| 247 |
} |
| 248 |
}); |
| 249 |
}()); |
| 250 |
|