Skip to content

Commit beb9d7c

Browse files
[fix] Refactored mixed-content check and fixed linting errors
- Extracted protocol validation into submitWithMixedContentCheck helper. - Used destructured setLoading to satisfy 'Must use destructuring' rule. - Fixed duplicated logic in componentDidMount, handleLogout, and handleSessionLogout. Addresses review feedback for #602
1 parent ecf0a4e commit beb9d7c

1 file changed

Lines changed: 21 additions & 30 deletions

File tree

client/components/status/status.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ import handleSession from "../../utils/session";
3838
import getPlanSelection from "../../utils/get-plan-selection";
3939
import getPlans from "../../utils/get-plans";
4040

41+
const checkMixedContent = (actionUrl) => {
42+
if (
43+
window.location.protocol === "https:" &&
44+
actionUrl &&
45+
typeof actionUrl === "string" &&
46+
actionUrl.startsWith("http:")
47+
) {
48+
throw new Error(
49+
"Mixed Content: Cannot submit insecure HTTP form from a secure HTTPS page.",
50+
);
51+
}
52+
};
53+
4154
export default class Status extends React.Component {
4255
constructor(props) {
4356
super(props);
@@ -235,18 +248,10 @@ export default class Status extends React.Component {
235248
);
236249
this.notifyCpLogin(userData);
237250
try {
238-
const formAction = new URL(this.loginFormRef.current.action);
239-
if (
240-
window.location.protocol === "https:" &&
241-
formAction.protocol === "http:"
242-
) {
243-
throw new Error(
244-
"Mixed Content: Cannot submit insecure HTTP form from a secure HTTPS page.",
245-
);
246-
}
251+
checkMixedContent(this.loginFormRef.current.action);
247252
this.loginFormRef.current.submit();
248253
} catch (error) {
249-
this.context.setLoading(false);
254+
setLoading(false);
250255
toast.error(`Security/Network Error: ${error.message}`);
251256
console.error("Mixed Content Exception:", error);
252257
}
@@ -604,18 +609,10 @@ export default class Status extends React.Component {
604609
cookies,
605610
);
606611
try {
607-
const formAction = new URL(this.logoutFormRef.current.action);
608-
if (
609-
window.location.protocol === "https:" &&
610-
formAction.protocol === "http:"
611-
) {
612-
throw new Error(
613-
"Mixed Content: Cannot submit insecure HTTP form from a secure HTTPS page.",
614-
);
615-
}
612+
checkMixedContent(this.logoutFormRef.current.action);
616613
this.logoutFormRef.current.submit();
617614
} catch (error) {
618-
this.context.setLoading(false);
615+
setLoading(false);
619616
toast.error(`Security/Network Error: ${error.message}`);
620617
console.error("Mixed Content Exception:", error);
621618
}
@@ -931,19 +928,13 @@ export default class Status extends React.Component {
931928
const {setLoading} = this.context;
932929
if (this.logoutFormRef && this.logoutFormRef.current) {
933930
try {
934-
const formAction = new URL(this.logoutFormRef.current.action);
935-
if (
936-
window.location.protocol === "https:" &&
937-
formAction.protocol === "http:"
938-
) {
939-
throw new Error(
940-
"Mixed Content: Cannot submit insecure HTTP form from a secure HTTPS page.",
941-
);
942-
}
931+
checkMixedContent(this.logoutFormRef.current.action);
943932
this.logoutFormRef.current.submit();
944933
} catch (error) {
945934
setLoading(false);
946-
toast.error(error.message);
935+
toast.error(`Security/Network Error: ${error.message}`);
936+
console.error("Mixed Content Exception:", error);
937+
return;
947938
}
948939
}
949940
setLoading(true);

0 commit comments

Comments
 (0)