| @@ -174,18 +174,35 @@ | ||
| 174 | 174 | |
| 175 | 175 | function uwp_ajax_login($this) { |
| 176 | 176 | |
| 177 | 177 | $('#uwp_login_modal .uwp-login-ajax-notice').remove(); |
| 178 | + $('#uwp_login_modal form.uwp-login-form').prev('.alert').remove(); | |
| 178 | 179 | |
| 179 | 180 | var data = jQuery($this).serialize()+ "&action=uwp_ajax_login"; |
| 180 | 181 | |
| 181 | 182 | jQuery.post(uwp_localize_data.ajaxurl, data, function(response) { |
| 182 | - response = jQuery.parseJSON(response); | |
| 183 | + if (typeof response === 'string') { | |
| 184 | + response = jQuery.parseJSON(response); | |
| 185 | + } | |
| 183 | 186 | |
| 184 | - if(response.error){ | |
| 185 | - $('#uwp_login_modal form.uwp-login-form').before(response.message); | |
| 187 | + if(response.success === false || response.error){ | |
| 188 | + $('#uwp_login_modal form.uwp-login-form').before(response.data && response.data.message ? response.data.message : response.message); | |
| 189 | + } else if (response.success === true && response.data && response.data.is_2fa) { | |
| 190 | + $('#uwp_login_modal form.uwp-login-form').replaceWith(response.data.html); | |
| 191 | + $('#uwp_login_modal form.validate_2fa_form').on('submit', function(e) { | |
| 192 | + e.preventDefault(); | |
| 193 | + uwp_ajax_login_2fa('form.validate_2fa_form', ''); | |
| 194 | + }); | |
| 195 | + $('#uwp_login_modal form.validate_2fa_backup_codes_form').on('submit', function(e) { | |
| 196 | + e.preventDefault(); | |
| 197 | + uwp_ajax_login_2fa('form.validate_2fa_backup_codes_form', ''); | |
| 198 | + }); | |
| 199 | + $('#uwp_login_modal .uwp-2fa-email-resend').on('click', function(e) { | |
| 200 | + e.preventDefault(); | |
| 201 | + uwp_ajax_login_2fa('form.validate_2fa_form', '&wp-2fa-email-code-resend=1', this); | |
| 202 | + }); | |
| 186 | 203 | } else { |
| 187 | - $('#uwp_login_modal form.uwp-login-form').before(response.message); | |
| 204 | + $('#uwp_login_modal form.uwp-login-form').before(response.data && response.data.message ? response.data.message : response.message); | |
| 188 | 205 | setTimeout(function(){location.reload()}, 1200) |
| 189 | 206 | } |
| 190 | 207 | |
| 191 | 208 | }); |
| @@ -190,8 +207,36 @@ | ||
| 190 | 207 | |
| 191 | 208 | }); |
| 192 | 209 | } |
| 193 | 210 | |
| 211 | + function uwp_ajax_login_2fa(type, fields, btnEl) { | |
| 212 | + $('#uwp_login_modal .uwp-login-ajax-notice').remove(); | |
| 213 | + $('#uwp_login_modal ' + type).prev('.alert').remove(); | |
| 214 | + | |
| 215 | + var data = jQuery('#uwp_login_modal ' + type).serialize() + '&action=uwp_ajax_login_process_2fa' + fields; | |
| 216 | + | |
| 217 | + jQuery.post(uwp_localize_data.ajaxurl, data, function(response) { | |
| 218 | + if (typeof response === 'string') { | |
| 219 | + response = jQuery.parseJSON(response); | |
| 220 | + } | |
| 221 | + | |
| 222 | + if (response.success === false || response.error) { | |
| 223 | + $('#uwp_login_modal ' + type).before(response.data && response.data.message ? response.data.message : response.message); | |
| 224 | + $('#uwp_login_modal ' + type).find('.uwp-2fa-submit').prop('disabled', false); | |
| 225 | + if (btnEl) { | |
| 226 | + $(btnEl).prop('disabled', false); | |
| 227 | + } | |
| 228 | + } else { | |
| 229 | + $('#uwp_login_modal ' + type).before(response.data && response.data.message ? response.data.message : response.message); | |
| 230 | + if (response.data && response.data.redirect) { | |
| 231 | + setTimeout(function(){location.href = response.data.redirect}, 1200); | |
| 232 | + } else { | |
| 233 | + setTimeout(function(){location.reload()}, 1200); | |
| 234 | + } | |
| 235 | + } | |
| 236 | + }); | |
| 237 | + } | |
| 238 | + | |
| 194 | 239 | }); |
| 195 | 240 | }( jQuery, window )); |
| 196 | 241 | |
| 197 | 242 | function uwp_nl2br(str, is_xhtml) { |
| @@ -440,9 +485,9 @@ | ||
| 440 | 485 | }); |
| 441 | 486 | |
| 442 | 487 | jQuery( '.uwp-auth-modal .modal-content .uwp-2fa-email-resend' ).on( 'click', function( e ) { |
| 443 | 488 | e.preventDefault(e); |
| 444 | - uwp_modal_login_form_2fa_process('form.validate_2fa_form', '&wp-2fa-email-code-resend=1'); | |
| 489 | + uwp_modal_login_form_2fa_process('form.validate_2fa_form', '&wp-2fa-email-code-resend=1', jQuery(this)); | |
| 445 | 490 | }); |
| 446 | 491 | } else { |
| 447 | 492 | if(data.data.redirect){ |
| 448 | 493 | setTimeout(function(){ |
| @@ -464,11 +509,13 @@ | ||
| 464 | 509 | } |
| 465 | 510 | }); |
| 466 | 511 | } |
| 467 | 512 | |
| 468 | -function uwp_modal_login_form_2fa_process(type, fields){ | |
| 513 | +function uwp_modal_login_form_2fa_process(type, fields, $button){ | |
| 469 | 514 | var data = jQuery(".modal-content " + type).serialize() + '&action=uwp_ajax_login_process_2fa'+fields; |
| 470 | - $button = jQuery('.uwp-auth-modal .modal-content '+type).find('.uwp-2fa-submit'); | |
| 515 | + if (!$button) { | |
| 516 | + $button = jQuery('.uwp-auth-modal .modal-content '+type).find('.uwp-2fa-submit'); | |
| 517 | + } | |
| 471 | 518 | $button_text = $button.html(); |
| 472 | 519 | jQuery.ajax({ |
| 473 | 520 | type: "POST", |
| 474 | 521 | url: uwp_localize_data.ajaxurl, |
| @@ -885,5 +932,107 @@ | ||
| 885 | 932 | return true; |
| 886 | 933 | } else { |
| 887 | 934 | return false; |
| 888 | 935 | } |
| 889 | -} | |
| 936 | +} | |
| 937 | + | |
| 938 | +/** | |
| 939 | + * Use the same two-step AJAX login flow on an in-page form as the login modal. | |
| 940 | + * This lets Wordfence validate the password first and return a separate 2FA form. | |
| 941 | + */ | |
| 942 | +(function ($) { | |
| 943 | + $(document).ready(function () { | |
| 944 | + if (!uwp_localize_data.wordfence_2fa_active) { | |
| 945 | + return; | |
| 946 | + } | |
| 947 | + | |
| 948 | + $('form.uwp-login-form').each(function () { | |
| 949 | + var $form = $(this); | |
| 950 | + | |
| 951 | + if ($form.closest('#uwp_login_modal, .uwp-auth-modal').length) { | |
| 952 | + return; | |
| 953 | + } | |
| 954 | + | |
| 955 | + $form.on('submit.uwpPageLogin', function (e) { | |
| 956 | + e.preventDefault(); | |
| 957 | + uwp_page_login_process($form); | |
| 958 | + }); | |
| 959 | + }); | |
| 960 | + }); | |
| 961 | + | |
| 962 | + function uwp_page_login_process($form) { | |
| 963 | + var $submit = $form.find('.uwp_login_submit'); | |
| 964 | + var buttonText = $submit.html(); | |
| 965 | + | |
| 966 | + $form.prev('.alert').remove(); | |
| 967 | + $submit.prop('disabled', true); | |
| 968 | + | |
| 969 | + $.post(uwp_localize_data.ajaxurl, $form.serialize() + '&action=uwp_ajax_login', function (response) { | |
| 970 | + if (typeof response === 'string') { | |
| 971 | + response = $.parseJSON(response); | |
| 972 | + } | |
| 973 | + | |
| 974 | + if (response.success === true && response.data && response.data.is_2fa) { | |
| 975 | + var $twoFactorMarkup = $(response.data.html); | |
| 976 | + var $twoFactorForm = $twoFactorMarkup.filter('form.validate_2fa_form') | |
| 977 | + .add($twoFactorMarkup.find('form.validate_2fa_form')).first(); | |
| 978 | + | |
| 979 | + $form.replaceWith($twoFactorMarkup); | |
| 980 | + bind_2fa_form($twoFactorForm); | |
| 981 | + $twoFactorForm.find('input:visible:enabled:first').trigger('focus'); | |
| 982 | + return; | |
| 983 | + } | |
| 984 | + | |
| 985 | + if (response.success === true) { | |
| 986 | + if (response.data && response.data.message) { | |
| 987 | + $form.before(response.data.message); | |
| 988 | + } | |
| 989 | + window.setTimeout(function () { | |
| 990 | + if (response.data && response.data.redirect) { | |
| 991 | + window.location.href = response.data.redirect; | |
| 992 | + } else { | |
| 993 | + window.location.reload(); | |
| 994 | + } | |
| 995 | + }, 1000); | |
| 996 | + return; | |
| 997 | + } | |
| 998 | + | |
| 999 | + $form.before(response.data && response.data.message ? response.data.message : response.message); | |
| 1000 | + $submit.html(buttonText).prop('disabled', false); | |
| 1001 | + document.dispatchEvent(new Event('ayecode_reset_captcha')); | |
| 1002 | + }).fail(function () { | |
| 1003 | + $submit.html(buttonText).prop('disabled', false); | |
| 1004 | + }); | |
| 1005 | + } | |
| 1006 | + | |
| 1007 | + function bind_2fa_form($form) { | |
| 1008 | + $form.on('submit.uwpPageLogin2fa', function (e) { | |
| 1009 | + e.preventDefault(); | |
| 1010 | + | |
| 1011 | + var $submit = $form.find('.uwp-2fa-submit'); | |
| 1012 | + $form.prev('.alert').remove(); | |
| 1013 | + $submit.prop('disabled', true); | |
| 1014 | + | |
| 1015 | + $.post(uwp_localize_data.ajaxurl, $form.serialize() + '&action=uwp_ajax_login_process_2fa', function (response) { | |
| 1016 | + if (typeof response === 'string') { | |
| 1017 | + response = $.parseJSON(response); | |
| 1018 | + } | |
| 1019 | + | |
| 1020 | + $form.before(response.data && response.data.message ? response.data.message : response.message); | |
| 1021 | + | |
| 1022 | + if (response.success === true) { | |
| 1023 | + window.setTimeout(function () { | |
| 1024 | + if (response.data && response.data.redirect) { | |
| 1025 | + window.location.href = response.data.redirect; | |
| 1026 | + } else { | |
| 1027 | + window.location.reload(); | |
| 1028 | + } | |
| 1029 | + }, 1000); | |
| 1030 | + } else { | |
| 1031 | + $submit.prop('disabled', false); | |
| 1032 | + } | |
| 1033 | + }).fail(function () { | |
| 1034 | + $submit.prop('disabled', false); | |
| 1035 | + }); | |
| 1036 | + }); | |
| 1037 | + } | |
| 1038 | +})(jQuery); | |