yes
This commit is contained in:
parent
085de41194
commit
eb5e4f25a1
24 changed files with 1233 additions and 202 deletions
|
|
@ -7,12 +7,33 @@
|
|||
<span class="text-red-600"> You can't change anything here </span>
|
||||
</div>
|
||||
|
||||
<div class="mb-1 text-gray-700 rounded-sm border-2 outline-lime-100">
|
||||
<label class="inline font-medium mb-1 text-gray-700">AccountType: </label>
|
||||
<span id="accountType" class="font-medium"></span>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Login Name -->
|
||||
<div id="loginNameWrapper" class="py-2">
|
||||
<div id="loginNameWrapper" class="py-2" hidden>
|
||||
<label class="block font-medium mb-1 text-gray-700">Login Name</label>
|
||||
<div id="loginNameBox" class="font-medium mb-1 text-gray-700 rounded-sm border-2 outline-lime-100"></div>
|
||||
</div>
|
||||
|
||||
<!-- Login Name -->
|
||||
<div id="providerWrapper" class="py-2 mb-1 border-2 border-green-400 rounded-sm" hidden>
|
||||
<span class="py-2 mb-1 text-gray-700 text-lg">Provider</span>
|
||||
<div class="flex items-center justify-center gap-4">
|
||||
<div>
|
||||
<label class="block font-medium mb-1 text-gray-700">Name</label>
|
||||
<div id="providerNameBox"
|
||||
class="font-medium mb-1 text-gray-700"></div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="block font-medium mb-1 text-gray-700">User</label>
|
||||
<div id="providerUserBox"
|
||||
class="font-medium mb-1 text-gray-700"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Display Name -->
|
||||
<div id="displayNameWrapper" class="py-2">
|
||||
<label class="block font-medium mb-1 text-gray-700">Display Name</label>
|
||||
|
|
@ -35,7 +56,7 @@
|
|||
</div>
|
||||
|
||||
<!-- TOTP -->
|
||||
<div class="border rounded p-4">
|
||||
<div class="border rounded p-4" id="totpWrapper">
|
||||
<h2 class="font-semibold text-lg mb-2">Two-Factor Authentication (TOTP)</h2>
|
||||
|
||||
<div class="flex items-center justify-between">
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { addRoute, navigateTo, setTitle } from "@app/routing";
|
||||
import { showError } from "@app/toast";
|
||||
import { addRoute, handleRoute, navigateTo, setTitle } from "@app/routing";
|
||||
import { showError, showSuccess } from "@app/toast";
|
||||
import page from "./profile.html?raw";
|
||||
import { updateUser } from "@app/auth";
|
||||
import { isNullish } from "@app/utils";
|
||||
|
|
@ -38,176 +38,219 @@ export async function renderOAuth2QRCode(
|
|||
});
|
||||
canvas.style.width = "";
|
||||
canvas.style.height = "";
|
||||
}
|
||||
|
||||
async function route(url: string, _args: { [k: string]: string }) {
|
||||
setTitle("Edit Profile");
|
||||
return {
|
||||
html: page,
|
||||
postInsert: async (app: HTMLElement | undefined) => {
|
||||
const user = await updateUser();
|
||||
if (isNullish(user)) return showError("No User");
|
||||
if (isNullish(app)) return showError("Failed to render");
|
||||
let totpState = await (async () => {
|
||||
let res = await client.statusOtp();
|
||||
if (res.kind === "success")
|
||||
return {
|
||||
enabled:
|
||||
(res.msg as string) === "statusOtp.success.enabled",
|
||||
secret:
|
||||
(res.msg as string) === "statusOtp.success.enabled"
|
||||
? res.payload.secret
|
||||
: null,
|
||||
};
|
||||
else {
|
||||
showError("Failed to get OTP status");
|
||||
return {
|
||||
enabled: false,
|
||||
secret: null,
|
||||
};
|
||||
}
|
||||
})();
|
||||
// ---- Simulated State ----
|
||||
let totpEnabled = totpState.enabled;
|
||||
let totpSecret = totpState.secret; // would come from backend
|
||||
|
||||
let guestBox = app.querySelector<HTMLDivElement>("#isGuestBox")!;
|
||||
let displayNameWrapper = app.querySelector<HTMLDivElement>(
|
||||
"#displayNameWrapper",
|
||||
)!;
|
||||
let displayNameBox =
|
||||
app.querySelector<HTMLInputElement>("#displayNameBox")!;
|
||||
let displayNameButton =
|
||||
app.querySelector<HTMLButtonElement>("#displayNameButton")!;
|
||||
let loginNameWrapper =
|
||||
app.querySelector<HTMLDivElement>("#loginNameWrapper")!;
|
||||
let loginNameBox =
|
||||
app.querySelector<HTMLDivElement>("#loginNameBox")!;
|
||||
let passwordWrapper =
|
||||
app.querySelector<HTMLDivElement>("#passwordWrapper")!;
|
||||
let passwordBox =
|
||||
app.querySelector<HTMLInputElement>("#passwordBox")!;
|
||||
let passwordButton =
|
||||
app.querySelector<HTMLButtonElement>("#passwordButton")!;
|
||||
|
||||
if (!isNullish(user.selfInfo?.loginName))
|
||||
loginNameBox.innerText = user.selfInfo?.loginName;
|
||||
else
|
||||
loginNameBox.innerHTML =
|
||||
'<span class="text-red-600 font-bold mb-1">You don\'t have a login name</span>';
|
||||
displayNameBox.value = user.name;
|
||||
|
||||
guestBox.hidden = !user.guest;
|
||||
|
||||
// ---- DOM Elements ----
|
||||
const totpStatusText = app.querySelector("#totpStatusText")!;
|
||||
const enableBtn =
|
||||
app.querySelector<HTMLButtonElement>("#enableTotp")!;
|
||||
const disableBtn =
|
||||
app.querySelector<HTMLButtonElement>("#disableTotp")!;
|
||||
const showSecretBtn =
|
||||
app.querySelector<HTMLButtonElement>("#showSecret")!;
|
||||
const secretBox = app.querySelector("#totpSecretBox")!;
|
||||
const secretText =
|
||||
app.querySelector<HTMLDivElement>("#totpSecretText")!;
|
||||
const secretCanvas =
|
||||
app.querySelector<HTMLCanvasElement>("#totpSecretCanvas")!;
|
||||
|
||||
if (user.guest) {
|
||||
for (let c of passwordButton.classList.values()) {
|
||||
if (c.startsWith("bg-") || c.startsWith("hover:bg-"))
|
||||
passwordButton.classList.remove(c);
|
||||
}
|
||||
passwordButton.disabled = true;
|
||||
passwordButton.classList.add(
|
||||
"bg-gray-700",
|
||||
"hover:bg-gray-700",
|
||||
);
|
||||
|
||||
passwordBox.disabled = true;
|
||||
passwordBox.classList.add("color-white");
|
||||
|
||||
for (let c of displayNameButton.classList.values()) {
|
||||
if (c.startsWith("bg-") || c.startsWith("hover:bg-"))
|
||||
displayNameButton.classList.remove(c);
|
||||
}
|
||||
displayNameButton.disabled = true;
|
||||
displayNameButton.classList.add("bg-gray-700");
|
||||
displayNameButton.classList.add("color-white");
|
||||
|
||||
displayNameBox.disabled = true;
|
||||
displayNameBox.classList.add("color-white");
|
||||
|
||||
for (let c of enableBtn.classList.values()) {
|
||||
if (c.startsWith("bg-") || c.startsWith("hover:bg-"))
|
||||
enableBtn.classList.remove(c);
|
||||
}
|
||||
for (let c of disableBtn.classList.values()) {
|
||||
if (c.startsWith("bg-") || c.startsWith("hover:bg-"))
|
||||
disableBtn.classList.remove(c);
|
||||
}
|
||||
for (let c of showSecretBtn.classList.values()) {
|
||||
if (c.startsWith("bg-") || c.startsWith("hover:bg-"))
|
||||
showSecretBtn.classList.remove(c);
|
||||
}
|
||||
enableBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
disableBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
showSecretBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
|
||||
enableBtn.disabled = true;
|
||||
disableBtn.disabled = true;
|
||||
showSecretBtn.disabled = true;
|
||||
function removeBgColor(...elem: HTMLElement[]) {
|
||||
for (let e of elem) {
|
||||
for (let c of e.classList.values()) {
|
||||
if (c.startsWith("bg-") || c.startsWith("hover:bg-"))
|
||||
e.classList.remove(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Update UI ----
|
||||
function refreshTotpUI() {
|
||||
if (totpEnabled) {
|
||||
totpStatusText.textContent = "Status: Enabled";
|
||||
async function route(url: string, _args: { [k: string]: string }) {
|
||||
setTitle("Edit Profile");
|
||||
return {
|
||||
html: page,
|
||||
postInsert: async (app: HTMLElement | undefined) => {
|
||||
const user = await updateUser();
|
||||
if (isNullish(user)) return showError("No User");
|
||||
if (isNullish(app)) return showError("Failed to render");
|
||||
let totpState = await (async () => {
|
||||
let res = await client.statusOtp();
|
||||
if (res.kind === "success")
|
||||
return {
|
||||
enabled:
|
||||
(res.msg as string) === "statusOtp.success.enabled",
|
||||
secret:
|
||||
(res.msg as string) === "statusOtp.success.enabled"
|
||||
? res.payload.secret
|
||||
: null,
|
||||
};
|
||||
else {
|
||||
showError("Failed to get OTP status");
|
||||
return {
|
||||
enabled: false,
|
||||
secret: null,
|
||||
};
|
||||
}
|
||||
})();
|
||||
// ---- Simulated State ----
|
||||
let totpEnabled = totpState.enabled;
|
||||
let totpSecret = totpState.secret; // would come from backend
|
||||
|
||||
enableBtn.classList.add("hidden");
|
||||
disableBtn.classList.remove("hidden");
|
||||
showSecretBtn.classList.remove("hidden");
|
||||
} else {
|
||||
totpStatusText.textContent = "Status: Disabled";
|
||||
let guestBox = app.querySelector<HTMLDivElement>("#isGuestBox")!;
|
||||
let displayNameWrapper = app.querySelector<HTMLDivElement>(
|
||||
"#displayNameWrapper",
|
||||
)!;
|
||||
let displayNameBox =
|
||||
app.querySelector<HTMLInputElement>("#displayNameBox")!;
|
||||
let displayNameButton =
|
||||
app.querySelector<HTMLButtonElement>("#displayNameButton")!;
|
||||
let loginNameWrapper =
|
||||
app.querySelector<HTMLDivElement>("#loginNameWrapper")!;
|
||||
let loginNameBox =
|
||||
app.querySelector<HTMLDivElement>("#loginNameBox")!;
|
||||
let passwordWrapper =
|
||||
app.querySelector<HTMLDivElement>("#passwordWrapper")!;
|
||||
let passwordBox =
|
||||
app.querySelector<HTMLInputElement>("#passwordBox")!;
|
||||
let passwordButton =
|
||||
app.querySelector<HTMLButtonElement>("#passwordButton")!;
|
||||
|
||||
enableBtn.classList.remove("hidden");
|
||||
disableBtn.classList.add("hidden");
|
||||
showSecretBtn.classList.add("hidden");
|
||||
secretBox.classList.add("hidden");
|
||||
}
|
||||
}
|
||||
let providerWrapper =
|
||||
app.querySelector<HTMLDivElement>("#providerWrapper")!;
|
||||
let providerNameBox =
|
||||
app.querySelector<HTMLDivElement>("#providerNameBox")!;
|
||||
let providerUserBox =
|
||||
app.querySelector<HTMLDivElement>("#providerUserBox")!;
|
||||
|
||||
// ---- Button Events ----
|
||||
enableBtn.onclick = async () => {
|
||||
let res = await client.enableOtp();
|
||||
if (res.kind === "success") {
|
||||
navigateTo(url);
|
||||
} else {
|
||||
showError(`failed to activate OTP: ${res.msg}`);
|
||||
}
|
||||
let accountTypeBox =
|
||||
app.querySelector<HTMLDivElement>("#accountType")!;
|
||||
displayNameBox.value = user.name;
|
||||
|
||||
guestBox.hidden = !user.guest;
|
||||
|
||||
// ---- DOM Elements ----
|
||||
const totpStatusText = app.querySelector("#totpStatusText")!;
|
||||
const enableBtn =
|
||||
app.querySelector<HTMLButtonElement>("#enableTotp")!;
|
||||
const disableBtn =
|
||||
app.querySelector<HTMLButtonElement>("#disableTotp")!;
|
||||
const showSecretBtn =
|
||||
app.querySelector<HTMLButtonElement>("#showSecret")!;
|
||||
const secretBox = app.querySelector("#totpSecretBox")!;
|
||||
const secretText =
|
||||
app.querySelector<HTMLDivElement>("#totpSecretText")!;
|
||||
const secretCanvas =
|
||||
app.querySelector<HTMLCanvasElement>("#totpSecretCanvas")!;
|
||||
|
||||
if (user.guest) {
|
||||
for (let c of passwordButton.classList.values()) {
|
||||
if (c.startsWith("bg-") || c.startsWith("hover:bg-"))
|
||||
passwordButton.classList.remove(c);
|
||||
}
|
||||
let totpWrapper = app.querySelector<HTMLDivElement>("#totpWrapper")!;
|
||||
|
||||
if (user.guest) {
|
||||
removeBgColor(
|
||||
passwordButton,
|
||||
displayNameButton,
|
||||
enableBtn,
|
||||
disableBtn,
|
||||
showSecretBtn,
|
||||
);
|
||||
|
||||
passwordButton.classList.add(
|
||||
"bg-gray-700",
|
||||
"hover:bg-gray-700",
|
||||
);
|
||||
|
||||
passwordBox.disabled = true;
|
||||
passwordBox.classList.add("color-white");
|
||||
|
||||
displayNameButton.disabled = true;
|
||||
displayNameButton.classList.add("bg-gray-700", "color-white");
|
||||
|
||||
displayNameBox.disabled = true;
|
||||
displayNameBox.classList.add("color-white");
|
||||
enableBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
disableBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
showSecretBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
|
||||
enableBtn.disabled = true;
|
||||
disableBtn.disabled = true;
|
||||
showSecretBtn.disabled = true;
|
||||
|
||||
accountTypeBox.innerText = "Guest";
|
||||
} else if (!isNullish(user.selfInfo?.loginName)) {
|
||||
loginNameWrapper.hidden = false;
|
||||
loginNameBox.innerText = user.selfInfo.loginName;
|
||||
|
||||
accountTypeBox.innerText = "Normal";
|
||||
} else if (
|
||||
!isNullish(user.selfInfo?.providerId) &&
|
||||
!isNullish(user.selfInfo?.providerUser)
|
||||
) {
|
||||
providerWrapper.hidden = false;
|
||||
providerNameBox.innerText = user.selfInfo.providerId;
|
||||
providerUserBox.innerText = user.selfInfo.providerUser;
|
||||
|
||||
enableBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
disableBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
showSecretBtn.classList.add("bg-gray-700", "hover:bg-gray-700");
|
||||
|
||||
enableBtn.disabled = true;
|
||||
disableBtn.disabled = true;
|
||||
showSecretBtn.disabled = true;
|
||||
|
||||
removeBgColor(enableBtn, disableBtn, showSecretBtn);
|
||||
passwordWrapper.hidden = true;
|
||||
totpWrapper.hidden = true;
|
||||
|
||||
accountTypeBox.innerText = "Provider";
|
||||
}
|
||||
|
||||
// ---- Update UI ----
|
||||
function refreshTotpUI() {
|
||||
if (totpEnabled) {
|
||||
totpStatusText.textContent = "Status: Enabled";
|
||||
|
||||
enableBtn.classList.add("hidden");
|
||||
disableBtn.classList.remove("hidden");
|
||||
showSecretBtn.classList.remove("hidden");
|
||||
} else {
|
||||
totpStatusText.textContent = "Status: Disabled";
|
||||
|
||||
enableBtn.classList.remove("hidden");
|
||||
disableBtn.classList.add("hidden");
|
||||
showSecretBtn.classList.add("hidden");
|
||||
secretBox.classList.add("hidden");
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Button Events ----
|
||||
enableBtn.onclick = async () => {
|
||||
let res = await client.enableOtp();
|
||||
if (res.kind === "success") {
|
||||
navigateTo(url);
|
||||
} else {
|
||||
showError(`failed to activate OTP: ${res.msg}`);
|
||||
}
|
||||
};
|
||||
|
||||
disableBtn.onclick = async () => {
|
||||
let res = await client.disableOtp();
|
||||
if (res.kind === "success") {
|
||||
navigateTo(url);
|
||||
} else {
|
||||
showError(`failed to deactivate OTP: ${res.msg}`);
|
||||
}
|
||||
};
|
||||
|
||||
showSecretBtn.onclick = () => {
|
||||
if (!isNullish(totpSecret)) {
|
||||
secretText.textContent = totpSecret;
|
||||
renderOAuth2QRCode(secretCanvas, totpSecret);
|
||||
}
|
||||
secretBox.classList.toggle("hidden");
|
||||
};
|
||||
|
||||
displayNameButton.onclick = async () => {
|
||||
let req = await client.changeDisplayName({
|
||||
changeDisplayNameRequest: { name: displayNameBox.value },
|
||||
});
|
||||
if (req.kind === "success") {
|
||||
showSuccess("Successfully changed display name");
|
||||
handleRoute();
|
||||
} else {
|
||||
showError(`Failed to update: ${req.msg}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize UI state
|
||||
refreshTotpUI();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
disableBtn.onclick = async () => {
|
||||
let res = await client.disableOtp();
|
||||
if (res.kind === "success") {
|
||||
navigateTo(url);
|
||||
} else {
|
||||
showError(`failed to deactivate OTP: ${res.msg}`);
|
||||
}
|
||||
};
|
||||
|
||||
showSecretBtn.onclick = () => {
|
||||
if (!isNullish(totpSecret)) {
|
||||
secretText.textContent = totpSecret;
|
||||
renderOAuth2QRCode(secretCanvas, totpSecret);
|
||||
}
|
||||
secretBox.classList.toggle("hidden");
|
||||
};
|
||||
|
||||
// Initialize UI state
|
||||
refreshTotpUI();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
addRoute("/profile", route);
|
||||
addRoute("/profile", route);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue