1
0
Fork 0
VaultUI/src/pages/TOTP/NewTOTP.js

95 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-04-17 10:45:42 +01:00
import { Page } from "../../types/Page.js";
import { addNewTOTP } from "../../api.js";
import { setTitleElement, setPageContent, setErrorText, changePage } from "../../pageUtils.js";
import { makeElement } from "../../htmlUtils.js";
import { Margin } from "../../elements/Margin.js";
import { MarginInline } from "../../elements/MarginInline.js";
2021-04-15 13:01:58 +01:00
export class NewTOTPPage extends Page {
constructor() {
super();
}
goBack() {
2021-04-17 11:24:43 +01:00
changePage("TOTP");
2021-04-15 13:01:58 +01:00
}
render() {
setTitleElement(pageState);
let totpForm = makeElement({
tag: "form",
children: [
Margin(makeElement({
tag: "input",
class: ["uk-input", "uk-form-width-medium"],
attributes: {
required: true,
type: "text",
placeholder: "TOTP Name",
name: "name"
}
})),
makeElement({
tag: "p",
text: "You need either a key or a URI, URI prefered. Just scan the QR code and copy the UI."
}),
Margin(makeElement({
tag: "input",
class: ["uk-input", "uk-form-width-medium"],
attributes: {
type: "text",
placeholder: "URI",
name: "uri"
}
})),
Margin(makeElement({
tag: "input",
class: ["uk-input", "uk-form-width-medium"],
attributes: {
type: "text",
placeholder: "Key",
name: "key"
}
})),
makeElement({
tag: "p",
id: "errorText",
class: "uk-text-danger"
}),
MarginInline(makeElement({
tag: "button",
class: ["uk-button", "uk-button-primary"],
text: "Add",
attributes: {
type: "submit"
}
}))
]
});
setPageContent(totpForm);
totpForm.addEventListener("submit", function (e) {
e.preventDefault();
let formData = new FormData(totpForm);
let parms = {
url: formData.get("uri"),
key: formData.get("key").replaceAll("-", "").replaceAll(" ", "").toUpperCase(),
2021-04-15 13:01:58 +01:00
name: formData.get("name"),
generate: false
};
2021-04-18 10:42:57 +01:00
addNewTOTP(pageState.currentBaseMount, parms).then(_ => {
2021-04-17 11:24:43 +01:00
changePage("TOTP");
2021-04-15 13:01:58 +01:00
}).catch(e => {
setErrorText(`API Error: ${e.message}`);
});
});
}
get titleSuffix() {
return " (new)";
}
2021-04-15 13:01:58 +01:00
get name() {
return "Create New TOTP";
}
}