Skip to content

Commit 8e1aeb3

Browse files
authored
feat(auth): allow removal of password authentication (@fehmer) (#5499)
1 parent 6b352a5 commit 8e1aeb3

4 files changed

Lines changed: 77 additions & 7 deletions

File tree

frontend/src/html/pages/settings.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,9 @@
16221622
</button>
16231623
<button class="danger" id="emailPasswordAuth">update email</button>
16241624
<button class="danger" id="passPasswordAuth">update password</button>
1625+
<button class="danger" id="removePasswordAuth">
1626+
remove password authentication
1627+
</button>
16251628
</div>
16261629
</div>
16271630
<div class="section googleAuthSettings needsAccount hidden">

frontend/src/styles/settings.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,18 @@
457457
gap: 0.5rem;
458458
}
459459
}
460+
461+
&.passwordAuthSettings {
462+
.buttons {
463+
grid-template-rows: repeat(auto-fill, 1fr);
464+
grid-template-columns: repeat(2, minmax(4.5rem, 1fr));
465+
466+
#removePasswordAuth,
467+
#addPasswordAuth {
468+
grid-column: span 2;
469+
}
470+
}
471+
}
460472
}
461473
}
462474

frontend/src/ts/modals/simple-modals.ts

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type PopupKey =
104104
| "updatePassword"
105105
| "removeGoogleAuth"
106106
| "removeGithubAuth"
107+
| "removePasswordAuth"
107108
| "addPasswordAuth"
108109
| "deleteAccount"
109110
| "resetAccount"
@@ -133,6 +134,7 @@ const list: Record<PopupKey, SimpleModal | undefined> = {
133134
updatePassword: undefined,
134135
removeGoogleAuth: undefined,
135136
removeGithubAuth: undefined,
137+
removePasswordAuth: undefined,
136138
addPasswordAuth: undefined,
137139
deleteAccount: undefined,
138140
resetAccount: undefined,
@@ -301,13 +303,8 @@ class SimpleModal {
301303
})
302304
);
303305
} else if (input.type === "checkbox") {
304-
let html = `
305-
<input
306-
id="${id}"
307-
type="checkbox"
308-
class="${input.hidden ? "hidden" : ""}"
309-
${input.initVal ? 'checked="checked"' : ""}>
310-
`;
306+
let html = buildTag({ tagname, classes, attributes });
307+
311308
if (input.description !== undefined) {
312309
html += `<span>${input.description}</span>`;
313310
}
@@ -797,6 +794,54 @@ list.removeGithubAuth = new SimpleModal({
797794
},
798795
});
799796

797+
list.removePasswordAuth = new SimpleModal({
798+
id: "removePaswordAuth",
799+
title: "Remove Password authentication",
800+
inputs: [
801+
{
802+
type: "checkbox",
803+
label: `I understand I will lose access to my Monkeytype account if my Google/GitHub account is lost or disabled.`,
804+
},
805+
],
806+
onlineOnly: true,
807+
buttonText: "reauthenticate to remove",
808+
execFn: async (_thisPopup): Promise<ExecReturn> => {
809+
const reauth = await reauthenticate({
810+
excludeMethod: "password",
811+
});
812+
if (reauth.status !== 1) {
813+
return {
814+
status: reauth.status,
815+
message: reauth.message,
816+
};
817+
}
818+
819+
try {
820+
await unlink(reauth.user, "password");
821+
} catch (e) {
822+
const message = createErrorMessage(
823+
e,
824+
"Failed to remove password authentication"
825+
);
826+
return {
827+
status: -1,
828+
message,
829+
};
830+
}
831+
832+
Settings.updateAuthSections();
833+
834+
reloadAfter(3);
835+
return {
836+
status: 1,
837+
message: "Password authentication removed",
838+
};
839+
},
840+
beforeInitFn: (): void => {
841+
if (!isAuthenticated()) return;
842+
},
843+
});
844+
800845
list.updateName = new SimpleModal({
801846
id: "updateName",
802847
title: "Update name",
@@ -1621,6 +1666,7 @@ list.updateCustomTheme = new SimpleModal({
16211666
type: "checkbox",
16221667
initVal: false,
16231668
label: "Update custom theme to current colors",
1669+
optional: true,
16241670
},
16251671
],
16261672
buttonText: "update",
@@ -1766,6 +1812,7 @@ list.devGenerateData = new SimpleModal({
17661812
label: "create user",
17671813
description:
17681814
"if checked, user will be created with {username}@example.com and password: password",
1815+
optional: true,
17691816
},
17701817
{
17711818
type: "date",
@@ -1865,6 +1912,9 @@ $(".pageSettings #removeGoogleAuth").on("click", () => {
18651912
$(".pageSettings #removeGithubAuth").on("click", () => {
18661913
showPopup("removeGithubAuth");
18671914
});
1915+
$(".pageSettings #removePasswordAuth").on("click", () => {
1916+
showPopup("removePasswordAuth");
1917+
});
18681918

18691919
$("#resetSettingsButton").on("click", () => {
18701920
showPopup("resetSettings");

frontend/src/ts/pages/settings.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,11 @@ export function updateAuthSections(): void {
755755
$(
756756
".pageSettings .section.passwordAuthSettings #passPasswordAuth"
757757
).removeClass("hidden");
758+
if (googleProvider || githubProvider) {
759+
$(
760+
".pageSettings .section.passwordAuthSettings #removePasswordAuth"
761+
).removeClass("hidden");
762+
}
758763
} else {
759764
$(
760765
".pageSettings .section.passwordAuthSettings #addPasswordAuth"

0 commit comments

Comments
 (0)