Skip to content

Commit 33c28a7

Browse files
committed
wip: scope inline login captcha token selectors to prompt container and add reset on error
1 parent 0ca23fb commit 33c28a7

1 file changed

Lines changed: 114 additions & 93 deletions

File tree

assets/js/checkout.js

Lines changed: 114 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,61 +1209,68 @@
12091209
? this.email_address || ''
12101210
: this.username || '';
12111211

1212-
// Include captcha tokens if present on the page.
1213-
const login_data = {
1214-
username_or_email,
1215-
password: this.inline_login_password,
1216-
_wpnonce: jQuery('[name="_wpnonce"]').val()
1217-
};
1218-
1219-
const recaptcha_token = jQuery('input[name="g-recaptcha-response"]').filter(function() {
1220-
return this.value;
1221-
}).first().val();
1222-
const hcaptcha_token = jQuery('input[name="h-captcha-response"]').filter(function() {
1223-
return this.value;
1224-
}).first().val();
1225-
const cap_token = jQuery('input[name="cap-token"]').filter(function() {
1226-
return this.value;
1227-
}).first().val();
1228-
1229-
if (recaptcha_token) {
1230-
login_data[ 'g-recaptcha-response' ] = recaptcha_token;
1231-
}
1212+
// Include captcha tokens scoped to the inline login prompt container.
1213+
// Using scoped selectors avoids picking up the checkout form's captcha token.
1214+
const promptContainer = jQuery('#wu-inline-login-prompt-' + this.login_prompt_field);
1215+
const login_data = {
1216+
username_or_email,
1217+
password: this.inline_login_password,
1218+
_wpnonce: jQuery('[name="_wpnonce"]').val()
1219+
};
12321220

1233-
if (hcaptcha_token) {
1234-
login_data[ 'h-captcha-response' ] = hcaptcha_token;
1235-
}
1221+
const recaptcha_token = promptContainer.find('input[name="g-recaptcha-response"]').filter(function() {
1222+
return this.value;
1223+
}).first().val();
1224+
const hcaptcha_token = promptContainer.find('input[name="h-captcha-response"]').filter(function() {
1225+
return this.value;
1226+
}).first().val();
1227+
const cap_token = promptContainer.find('input[name="cap-token"]').filter(function() {
1228+
return this.value;
1229+
}).first().val();
12361230

1237-
if (cap_token) {
1238-
login_data[ 'cap-token' ] = cap_token;
1239-
}
1231+
if (recaptcha_token) {
1232+
login_data[ 'g-recaptcha-response' ] = recaptcha_token;
1233+
}
12401234

1241-
this.request('wu_inline_login', login_data, function(results) {
1235+
if (hcaptcha_token) {
1236+
login_data[ 'h-captcha-response' ] = hcaptcha_token;
1237+
}
12421238

1243-
that.logging_in = false;
1239+
if (cap_token) {
1240+
login_data[ 'cap-token' ] = cap_token;
1241+
}
12441242

1245-
if (results.success) {
1243+
this.request('wu_inline_login', login_data, function(results) {
12461244

1247-
// Login successful - reload page to show logged-in state
1248-
window.location.reload();
1245+
that.logging_in = false;
12491246

1250-
}
1247+
if (results.success) {
12511248

1252-
}, function(error) {
1249+
// Login successful - reload page to show logged-in state
1250+
window.location.reload();
12531251

1254-
that.logging_in = false;
1252+
}
12551253

1256-
if (error.responseJSON && error.responseJSON.data && error.responseJSON.data.message) {
1254+
}, function(error) {
12571255

1258-
that.login_error = error.responseJSON.data.message;
1256+
that.logging_in = false;
12591257

1260-
} else {
1258+
if (error.responseJSON && error.responseJSON.data && error.responseJSON.data.message) {
12611259

1262-
that.login_error = wu_checkout.i18n.login_failed || 'Login failed. Please try again.';
1260+
that.login_error = error.responseJSON.data.message;
12631261

1264-
}
1262+
} else {
12651263

1266-
});
1264+
that.login_error = wu_checkout.i18n.login_failed || 'Login failed. Please try again.';
1265+
1266+
}
1267+
1268+
// Reset inline login captcha widgets so the user can re-verify.
1269+
if (typeof window.wuCaptchaResetInlineLogin === 'function') {
1270+
window.wuCaptchaResetInlineLogin();
1271+
}
1272+
1273+
});
12671274

12681275
return false;
12691276

@@ -1336,22 +1343,27 @@
13361343

13371344
}
13381345

1339-
function handleError(error) {
1346+
function handleError(error) {
13401347

1341-
submitButton.disabled = false;
1342-
submitButton.textContent = wu_checkout.i18n.sign_in || 'Sign in';
1348+
submitButton.disabled = false;
1349+
submitButton.textContent = wu_checkout.i18n.sign_in || 'Sign in';
13431350

1344-
if (error.data && error.data.message) {
1351+
if (error.data && error.data.message) {
13451352

1346-
showError(error.data.message);
1353+
showError(error.data.message);
13471354

1348-
} else {
1355+
} else {
13491356

1350-
showError(wu_checkout.i18n.login_failed || 'Login failed. Please try again.');
1357+
showError(wu_checkout.i18n.login_failed || 'Login failed. Please try again.');
13511358

1352-
}
1359+
}
13531360

1354-
}
1361+
// Reset inline login captcha widgets so the user can re-verify.
1362+
if (typeof window.wuCaptchaResetInlineLogin === 'function') {
1363+
window.wuCaptchaResetInlineLogin();
1364+
}
1365+
1366+
}
13551367

13561368
function handleLogin(e) {
13571369

@@ -1375,52 +1387,61 @@
13751387

13761388
const username_or_email = fieldType === 'email' ? that.email_address : that.username;
13771389

1378-
// Include captcha tokens if present on the page.
1379-
const inline_login_data = {
1380-
username_or_email,
1381-
password,
1382-
_wpnonce: jQuery('[name="_wpnonce"]').val()
1383-
};
1384-
1385-
const recaptcha_val = jQuery('input[name="g-recaptcha-response"]').filter(function() {
1386-
return this.value;
1387-
}).first().val();
1388-
const hcaptcha_val = jQuery('input[name="h-captcha-response"]').filter(function() {
1389-
return this.value;
1390-
}).first().val();
1391-
const cap_val = jQuery('input[name="cap-token"]').filter(function() {
1392-
return this.value;
1393-
}).first().val();
1394-
1395-
if (recaptcha_val) {
1396-
inline_login_data[ 'g-recaptcha-response' ] = recaptcha_val;
1397-
}
1398-
1399-
if (hcaptcha_val) {
1400-
inline_login_data[ 'h-captcha-response' ] = hcaptcha_val;
1401-
}
1402-
1403-
if (cap_val) {
1404-
inline_login_data[ 'cap-token' ] = cap_val;
1405-
}
1406-
1407-
jQuery.ajax({
1408-
method: 'POST',
1409-
url: wu_checkout.late_ajaxurl + '&action=wu_inline_login',
1410-
data: inline_login_data,
1411-
success(results) {
1412-
1413-
if (results.success) {
1414-
1415-
window.location.reload();
1390+
// Include captcha tokens scoped to the inline login prompt container.
1391+
// Using scoped selectors avoids picking up the checkout form's captcha token.
1392+
const promptContainer = jQuery('#wu-inline-login-prompt-' + fieldType);
1393+
const inline_login_data = {
1394+
username_or_email,
1395+
password,
1396+
_wpnonce: jQuery('[name="_wpnonce"]').val()
1397+
};
1398+
1399+
const recaptcha_val = promptContainer.find('input[name="g-recaptcha-response"]').filter(function() {
1400+
return this.value;
1401+
}).first().val();
1402+
const hcaptcha_val = promptContainer.find('input[name="h-captcha-response"]').filter(function() {
1403+
return this.value;
1404+
}).first().val();
1405+
const cap_val = promptContainer.find('input[name="cap-token"]').filter(function() {
1406+
return this.value;
1407+
}).first().val();
1408+
1409+
if (recaptcha_val) {
1410+
inline_login_data[ 'g-recaptcha-response' ] = recaptcha_val;
1411+
}
1412+
1413+
if (hcaptcha_val) {
1414+
inline_login_data[ 'h-captcha-response' ] = hcaptcha_val;
1415+
}
1416+
1417+
if (cap_val) {
1418+
inline_login_data[ 'cap-token' ] = cap_val;
1419+
}
1420+
1421+
jQuery.ajax({
1422+
method: 'POST',
1423+
url: wu_checkout.late_ajaxurl + '&action=wu_inline_login',
1424+
data: inline_login_data,
1425+
success(results) {
1426+
1427+
if (results.success) {
1428+
1429+
window.location.reload();
1430+
1431+
} else {
1432+
handleError(results);
1433+
}
14161434

1417-
} else {
1418-
handleError(results);
1419-
}
1435+
},
1436+
error(xhr) {
1437+
handleError(xhr.responseJSON || {});
14201438

1421-
},
1422-
error: handleError
1423-
});
1439+
// Reset inline login captcha widgets so the user can re-verify.
1440+
if (typeof window.wuCaptchaResetInlineLogin === 'function') {
1441+
window.wuCaptchaResetInlineLogin();
1442+
}
1443+
}
1444+
});
14241445

14251446
return false;
14261447

0 commit comments

Comments
 (0)