jQuery(document).ready(function($) {
// Return value: whether to submit the form or not
function runGenerateOTPCall() {
var username = $('#user_login').val() || $('[name="log"]').val();
if(!username.length) return false;
// If this is a "lost password" form, then exit
if ($('#user_login').parents('#lostpasswordform, #resetpasswordform').length) return false;
if (simba_tfasettings.hasOwnProperty('spinnerimg')) {
var styling = 'float:right; margin:6px 12px;';
if ($('#theme-my-login #wp-submit').length >0) {
var styling = 'margin-left: 4px; position: relative; top: 4px; border:0px; box-shadow:none;';
}
$('#wp-submit').after('');
}
$.ajax({
url: simba_tfasettings.ajaxurl,
type: 'POST',
data: {
action: 'simbatfa-init-otp',
user: username
},
dataType: 'text',
success: function(resp) {
try {
var json_begins = resp.search('{"jsonstarter":"justhere"');
if (json_begins > -1) {
if (json_begins > 0) {
console.log("Expected JSON marker found at position: "+json_begins);
resp = resp.substring(json_begins);
}
} else {
console.log("Expected JSON marker not found");
console.log(resp);
}
response = $.parseJSON(resp);
if (response.hasOwnProperty('php_output')) {
console.log("PHP output was returned (follows)");
console.log(response.php_output);
}
if (response.hasOwnProperty('extra_output')) {
console.log("Extra output was returned (follows)");
console.log(response.extra_output);
}
if (response.status === true) {
// Don't bother to remove the spinner if the form is being submitted.
$('.simbaotp_spinner').remove();
console.log("Simba TFA: User has OTP enabled: showing OTP field");
tfaShowOTPField();
} else {
console.log("Simba TFA: User does not have OTP enabled: submitting form");
$('#wp-submit').parents('form:first').submit();
}
} catch(err) {
$('#login').html(resp);
console.log("Simba TFA: Error when processing response");
console.log(err);
console.log(resp);
}
},
error: function(jq_xhr, text_status, error_thrown) {
console.log("Simba TFA: AJAX error: "+error_thrown+": "+text_status);
console.log(jq_xhr);
if (jq_xhr.hasOwnProperty('responseText')) { console.log(jq_xhr.responseText);}
}
});
return true;
}
function tfaShowOTPField() {
//Hide all elements in sa browser safe way
$('#wp-submit').parents('form:first').find('p').each(function(i) {
$(this).css('visibility','hidden').css('position', 'absolute');
});
$('#wp-submit').attr('disabled', 'disabled');
//Add new field and controls
var html = '';
html += '';
html += '
' + simba_tfasettings.otp_login_help + '
'; html += ''; $('#wp-submit').parents('form:first').prepend(html); $('#simba_two_factor_auth').focus(); } var tfa_cb = function(e) { console.log("Simba TFA: form submit request"); var res = runGenerateOTPCall(); $('#wp-submit').parents('form:first').off(); if(!res) return true; e.preventDefault(); return false; }; $('#wp-submit').parents('form:first').on('submit', tfa_cb); });