Skip to content

Commit b59c741

Browse files
authored
Merge pull request #342 from Ultimate-Multisite/fix/password-strength-enforcement
Fix password strength not enforced on checkout (minStrength hardcoded)
2 parents d16ef5d + 42367a3 commit b59c741

49 files changed

Lines changed: 2147 additions & 1013 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

assets/css/checkout.min.css

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/checkout.js

Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,6 @@
795795
this.password_strength_checker = new window.WU_PasswordStrength({
796796
pass1: pass1_el,
797797
result: jQuery('#pass-strength-result'),
798-
minStrength: 3,
799798
onValidityChange(isValid) {
800799

801800
that.valid_password = isValid;
@@ -813,10 +812,18 @@
813812
}, 500),
814813
check_user_exists(field_type, value) {
815814

815+
// Don't let other field checks interfere with an active email prompt
816+
if (this.show_login_prompt && this.login_prompt_field === 'email' && field_type !== 'email') {
817+
return;
818+
}
819+
816820
// Don't check if value is too short
817821
if (! value || value.length < 3) {
818822

819-
this.show_login_prompt = false;
823+
if (this.login_prompt_field === field_type) {
824+
this.show_login_prompt = false;
825+
this.remove_field_error(field_type === 'email' ? 'email_address' : 'username');
826+
}
820827

821828
return;
822829

@@ -840,16 +847,23 @@
840847
that.show_login_prompt = true;
841848
that.login_prompt_field = field_type;
842849

843-
} else {
850+
that.add_field_error(field_type === 'email' ? 'email_address' : 'username', wu_checkout.i18n.email_exists);
851+
852+
} else if (that.login_prompt_field === field_type) {
844853

845854
that.show_login_prompt = false;
855+
that.remove_field_error(field_type === 'email' ? 'email_address' : 'username');
846856

847857
}
848858

849859
}, function(error) {
850860

851861
that.checking_user_exists = false;
852-
that.show_login_prompt = false;
862+
863+
if (that.login_prompt_field === field_type) {
864+
that.show_login_prompt = false;
865+
that.remove_field_error(field_type === 'email' ? 'email_address' : 'username');
866+
}
853867

854868
});
855869

@@ -914,6 +928,25 @@
914928

915929
return false;
916930

931+
},
932+
add_field_error(field_code, message) {
933+
934+
this.remove_field_error(field_code);
935+
936+
this.errors.push({
937+
code: field_code,
938+
message,
939+
});
940+
941+
},
942+
remove_field_error(field_code) {
943+
944+
this.errors = this.errors.filter(function(e) {
945+
946+
return e.code !== field_code;
947+
948+
});
949+
917950
},
918951
dismiss_login_prompt() {
919952

@@ -929,40 +962,56 @@
929962
// Setup handlers for both email and username field types
930963
[ 'email', 'username' ].forEach(function(fieldType) {
931964

965+
const loginPromptContainer = document.getElementById('wu-inline-login-prompt-' + fieldType);
966+
967+
if (! loginPromptContainer) {
968+
return;
969+
}
970+
971+
// Only attach handlers once per container
972+
if (loginPromptContainer.dataset.wuHandlersAttached) {
973+
return;
974+
}
975+
976+
loginPromptContainer.dataset.wuHandlersAttached = '1';
977+
932978
const passwordField = document.getElementById('wu-inline-login-password-' + fieldType);
933979
const submitButton = document.getElementById('wu-inline-login-submit-' + fieldType);
934980

935981
if (! passwordField || ! submitButton) {
936982
return;
937983
}
938984

939-
const dismissButton = document.getElementById('wu-dismiss-login-prompt-' + fieldType);
940985
const errorDiv = document.getElementById('wu-login-error-' + fieldType);
941-
const loginPromptContainer = document.getElementById('wu-inline-login-prompt-' + fieldType);
942986

943-
// Remove any existing listeners to avoid duplicates
944-
const newSubmitButton = submitButton.cloneNode(true);
945-
submitButton.parentNode.replaceChild(newSubmitButton, submitButton);
987+
function showError(message) {
988+
989+
errorDiv.textContent = message;
990+
errorDiv.classList.remove('wu-hidden');
991+
992+
}
993+
994+
function hideError() {
995+
996+
errorDiv.classList.add('wu-hidden');
997+
998+
}
946999

947-
const newPasswordField = passwordField.cloneNode(true);
948-
passwordField.parentNode.replaceChild(newPasswordField, passwordField);
9491000
function handleError(error) {
9501001

951-
newSubmitButton.disabled = false;
952-
newSubmitButton.textContent = wu_checkout.i18n.sign_in || 'Sign in';
1002+
submitButton.disabled = false;
1003+
submitButton.textContent = wu_checkout.i18n.sign_in || 'Sign in';
9531004

9541005
if (error.data && error.data.message) {
9551006

956-
errorDiv.textContent = error.data.message;
1007+
showError(error.data.message);
9571008

9581009
} else {
9591010

960-
errorDiv.textContent = wu_checkout.i18n.login_failed || 'Login failed. Please try again.';
1011+
showError(wu_checkout.i18n.login_failed || 'Login failed. Please try again.');
9611012

9621013
}
9631014

964-
errorDiv.style.display = 'block';
965-
9661015
}
9671016

9681017
function handleLogin(e) {
@@ -971,20 +1020,19 @@
9711020
e.stopPropagation();
9721021
e.stopImmediatePropagation();
9731022

974-
const password = newPasswordField.value;
1023+
const password = passwordField.value;
9751024

9761025
if (! password) {
9771026

978-
errorDiv.textContent = wu_checkout.i18n.password_required || 'Password is required';
979-
errorDiv.style.display = 'block';
1027+
showError(wu_checkout.i18n.password_required || 'Password is required');
9801028

9811029
return false;
9821030

9831031
}
9841032

985-
newSubmitButton.disabled = true;
986-
newSubmitButton.innerHTML = '<span class="spinner is-active wu-inline-block" style="float: none; width: 16px; height: 16px; margin: 0 4px 0 0;"></span>' + (wu_checkout.i18n.logging_in || 'Logging in...');
987-
errorDiv.style.display = 'none';
1033+
submitButton.disabled = true;
1034+
submitButton.innerHTML = '<span class="spinner is-active wu-inline-block" style="float: none; width: 16px; height: 16px; margin: 0 4px 0 0;"></span>' + (wu_checkout.i18n.logging_in || 'Logging in...');
1035+
hideError();
9881036

9891037
const username_or_email = fieldType === 'email' ? that.email_address : that.username;
9901038

@@ -1015,31 +1063,27 @@
10151063
}
10161064

10171065
// Stop all events from bubbling out of the login prompt
1018-
if (loginPromptContainer) {
1019-
1020-
loginPromptContainer.addEventListener('click', function(e) {
1066+
loginPromptContainer.addEventListener('click', function(e) {
10211067

1022-
e.stopPropagation();
1023-
1024-
});
1068+
e.stopPropagation();
10251069

1026-
loginPromptContainer.addEventListener('keydown', function(e) {
1070+
});
10271071

1028-
e.stopPropagation();
1072+
loginPromptContainer.addEventListener('keydown', function(e) {
10291073

1030-
});
1074+
e.stopPropagation();
10311075

1032-
loginPromptContainer.addEventListener('keyup', function(e) {
1076+
});
10331077

1034-
e.stopPropagation();
1078+
loginPromptContainer.addEventListener('keyup', function(e) {
10351079

1036-
});
1080+
e.stopPropagation();
10371081

1038-
}
1082+
});
10391083

1040-
newSubmitButton.addEventListener('click', handleLogin);
1084+
submitButton.addEventListener('click', handleLogin);
10411085

1042-
newPasswordField.addEventListener('keydown', function(e) {
1086+
passwordField.addEventListener('keydown', function(e) {
10431087

10441088
if (e.key === 'Enter') {
10451089

@@ -1049,20 +1093,6 @@
10491093

10501094
});
10511095

1052-
if (dismissButton) {
1053-
1054-
dismissButton.addEventListener('click', function(e) {
1055-
1056-
e.preventDefault();
1057-
e.stopPropagation();
1058-
that.show_login_prompt = false;
1059-
that.inline_login_password = '';
1060-
newPasswordField.value = '';
1061-
1062-
});
1063-
1064-
}
1065-
10661096
});
10671097

10681098
},
@@ -1078,6 +1108,11 @@
10781108
// Setup inline login handlers if prompt is visible
10791109
this.setup_inline_login_handlers();
10801110

1111+
// Re-initialize password strength if field appeared after mount
1112+
if (! this.password_strength_checker && jQuery('#field-password').length) {
1113+
this.init_password_strength();
1114+
}
1115+
10811116
});
10821117

10831118
},
@@ -1159,6 +1194,11 @@
11591194

11601195
},
11611196
watch: {
1197+
email_address: _.debounce(function(new_value) {
1198+
1199+
this.check_user_exists('email', new_value);
1200+
1201+
}, 500),
11621202
products(new_value, old_value) {
11631203

11641204
this.on_change_product(new_value, old_value);

0 commit comments

Comments
 (0)