Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: add change vault password

thecodrra853ae119a4a
diff --git a/apps/web/src/common/vault.js b/apps/web/src/common/vault.js
index 556d9d394..1da7839e2 100644
--- a/apps/web/src/common/vault.js
+++ b/apps/web/src/common/vault.js
@@ -1,15 +1,18 @@
import { db } from "../common";
import { showPasswordDialog } from "../components/dialogs/passworddialog";
+import { showToast } from "../utils/toast";
class Vault {
static createVault() {
- return showPasswordDialog("create_vault", (password) =>
- db.vault.create(password)
- );
+ return showPasswordDialog("create_vault", async ({ password }) => {
+ await db.vault.create(password);
+ await showToast("success", "Vault created.");
+ return true;
+ });
}
static unlockVault() {
- return showPasswordDialog("unlock_vault", (password) => {
+ return showPasswordDialog("unlock_vault", ({ password }) => {
return db.vault
.unlock(password)
.then(() => true)
@@ -17,9 +20,20 @@ class Vault {
});
}
+ static changeVaultPassword() {
+ return showPasswordDialog(
+ "change_password",
+ async ({ oldPassword, newPassword }) => {
+ await db.vault.changePassword(oldPassword, newPassword);
+ showToast("success", "Vault password changed.");
+ return true;
+ }
+ );
+ }
+
static unlockNote(id) {
return new Promise((resolve) => {
- return showPasswordDialog("unlock_note", (password) => {
+ return showPasswordDialog("unlock_note", ({ password }) => {
return db.vault
.remove(id, password)
.then(() => true)
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.js
index 017d44390..8e94ecda8 100644
--- a/apps/web/src/components/dialogs/dialog.js
+++ b/apps/web/src/components/dialogs/dialog.js
@@ -5,6 +5,7 @@ import ThemeProvider from "../theme-provider";
import * as Icon from "../icons";
import Modal from "react-modal";
import { useTheme } from "emotion-theming";
+import { getHashParam, setHashParam } from "../../utils/useHashParam";
function Dialog(props) {
const theme = useTheme();
@@ -76,9 +77,9 @@ function Dialog(props) {
mb={3}
sx={{ borderBottom: "1px solid", borderColor: "border" }}
>
- {props.icon && (
+ {/* {props.icon && (
<props.icon size={props.iconSize || 38} color="primary" />
- )}
+ )} */}
<Text
variant="heading"
textAlign="center"
@@ -103,6 +104,7 @@ function Dialog(props) {
>
{props.positiveButton && (
<RebassButton
+ {...props.positiveButton.props}
variant="primary"
data-test-id="dialog-yes"
sx={{ opacity: props.positiveButton.disabled ? 0.7 : 1 }}
@@ -142,13 +144,23 @@ export default Dialog;
export function showDialog(dialog) {
const root = document.getElementById("dialogContainer");
- const perform = (resolve, result) => {
- ReactDOM.unmountComponentAtNode(root);
- resolve(result);
- };
+
if (root) {
return new Promise((resolve) => {
- const PropDialog = dialog(perform.bind(this, resolve));
+ const perform = (result) => {
+ window.removeEventListener("hashchange", onHashChange);
+ ReactDOM.unmountComponentAtNode(root);
+ resolve(result);
+ };
+ window.addEventListener("hashchange", onHashChange);
+
+ function onHashChange() {
+ if (!getHashParam("type")) perform(false);
+ }
+
+ setHashParam({ type: "dialog" });
+
+ const PropDialog = dialog(perform);
ReactDOM.render(<ThemeProvider>{PropDialog}</ThemeProvider>, root);
});
}
diff --git a/apps/web/src/components/dialogs/password-dialog.js b/apps/web/src/components/dialogs/password-dialog.js
index 1b5373519..a00b1c2dd 100644
--- a/apps/web/src/components/dialogs/password-dialog.js
+++ b/apps/web/src/components/dialogs/password-dialog.js
@@ -1,21 +1,37 @@
-import React, { useState, useRef, useCallback } from "react";
-import { Box, Text, Flex } from "rebass";
-import { Input } from "@rebass/forms";
+import React, { useState, useCallback, useMemo } from "react";
+import { Box, Text } from "rebass";
import Dialog, { showDialog } from "./dialog";
import * as Icon from "../icons";
+import Field from "../field";
+const requiredValues = ["password", "newPassword", "oldPassword"];
function PasswordDialog(props) {
- const [isWrong, setIsWrong] = useState(false);
- const passwordRef = useRef();
- const submit = useCallback(async () => {
- const password = passwordRef.current.value;
- if (await props.validate(password)) {
- props.onDone();
- } else {
- setIsWrong(true);
- passwordRef.current.focus();
- }
- }, [setIsWrong, props]);
+ const { type } = props;
+ const [error, setError] = useState();
+ const [isLoading, setIsLoading] = useState(false);
+
+ const isChangePasswordDialog = useMemo(() => type === "change_password", [
+ type,
+ ]);
+
+ const submit = useCallback(
+ async (data) => {
+ setIsLoading(true);
+ setError(false);
+ try {
+ if (await props.validate(data)) {
+ props.onDone();
+ } else {
+ setError("Wrong password.");
+ }
+ } catch (e) {
+ setError("Wrong password.");
+ } finally {
+ setIsLoading(false);
+ }
+ },
+ [props]
+ );
return (
<Dialog
isOpen={true}
@@ -24,34 +40,59 @@ function PasswordDialog(props) {
icon={props.icon}
onClose={props.onCancel}
positiveButton={{
+ props: {
+ form: "passwordForm",
+ type: "submit",
+ },
text: props.positiveButtonText,
- onClick: submit,
+ loading: isLoading,
+ disabled: isLoading,
}}
negativeButton={{ text: "Cancel", onClick: props.onCancel }}
>
- <Box my={1}>
- <Input
- data-test-id="dialog-vault-pass"
- ref={passwordRef}
+ <Box
+ id="passwordForm"
+ as="form"
+ onSubmit={async (e) => {
+ e.preventDefault();
+ const data = requiredValues.reduce((prev, curr) => {
+ if (!e.target[curr]) return prev;
+ prev[curr] = e.target[curr].value;
+ return prev;
+ }, {});
+
+ setError();
+
+ await submit(data);
+ }}
+ >
+ <Field
autoFocus
- variant={isWrong ? "error" : "input"}
+ required
+ label={isChangePasswordDialog ? "Old password" : "Password"}
type="password"
- placeholder="Enter vault password"
- onKeyUp={async (e) => {
- if (e.key === "Enter") {
- await submit();
- } else {
- setIsWrong(false);
- }
- }}
+ autoComplete={
+ type === "create_vault" ? "new-password" : "current-password"
+ }
+ id={isChangePasswordDialog ? "oldPassword" : "password"}
+ name={isChangePasswordDialog ? "oldPassword" : "password"}
/>
- {isWrong && (
- <Flex alignItems="center" color="error" mt={2}>
- <Icon.Alert size={16} color="error" />
- <Text ml={1} fontSize={"subBody"}>
- Wrong password
- </Text>
- </Flex>
+
+ {isChangePasswordDialog ? (
+ <Field
+ required
+ label="New password"
+ type="password"
+ autoComplete="new-password"
+ id="newPassword"
+ name="newPassword"
+ />
+ ) : null}
+
+ {error && (
+ <Text mt={1} variant={"error"}>
+ {error}
+ </Text>
)}
</Box>
</Dialog>
@@ -63,23 +104,31 @@ function getDialogData(type) {
case "create_vault":
return {
title: "Create Your Vault",
- subtitle: "Your vault will encrypt everything locally.",
+ subtitle: "A vault stores your notes in a password-encrypted storage.",
icon: Icon.Vault,
- positiveButtonText: "Create my vault",
+ positiveButtonText: "Create vault",
};
case "unlock_vault":
return {
title: "Unlock your Vault",
subtitle: "Your vault will remain unlocked for 30 minutes.",
icon: Icon.Unlock,
- positiveButtonText: "Unlock my vault",
+ positiveButtonText: "Unlock vault",
};
case "unlock_note":
return {
- title: "Unlock your Note",
+ title: "Unlock Note",
subtitle: "Unlocking will make this note openly available.",
icon: Icon.Unlock,
- positiveButtonText: "Unlock this note",
+ positiveButtonText: "Unlock note",
+ };
+ case "change_password":
+ return {
+ title: "Change Vault Password",
+ subtitle:
+ "All locked notes will be re-encrypted with the new password.",
+ icon: Icon.Password,
+ positiveButtonText: "Change password",
};
default:
return;
@@ -90,6 +139,7 @@ export function showPasswordDialog(type, validate) {
const { title, subtitle, icon, positiveButtonText } = getDialogData(type);
return showDialog((perform) => (
<PasswordDialog
+ type={type}
title={title}
subtitle={subtitle}
icon={icon}
diff --git a/apps/web/src/components/field/index.js b/apps/web/src/components/field/index.js
new file mode 100644
index 000000000..7dd8798d4
--- /dev/null
+++ b/apps/web/src/components/field/index.js
@@ -0,0 +1,37 @@
+import React from "react";
+import { Flex } from "rebass";
+import { Input, Label } from "@rebass/forms";
+
+function Field(props) {
+ const {
+ id,
+ label,
+ type,
+ sx,
+ name,
+ required,
+ autoFocus,
+ autoComplete,
+ } = props;
+ return (
+ <Flex sx={sx} flexDirection="column">
+ <Label htmlFor={id} sx={{ fontSize: "title" }}>
+ {label}
+ </Label>
+ {/* <Label htmlFor={id} sx={{ fontSize: "body", color: "gray" }} mb={1}>
+ Hello I am secondary
+ </Label> */}
+ <Input
+ autoFocus={autoFocus}
+ required={required}
+ name={name}
+ id={id}
+ autoComplete={autoComplete}
+ type={type || "text"}
+ mt={1}
+ />
+ </Flex>
+ );
+}
+
+export default Field;
diff --git a/apps/web/src/views/settings.js b/apps/web/src/views/settings.js
index 0e749bb23..941d6211e 100644
--- a/apps/web/src/views/settings.js
+++ b/apps/web/src/views/settings.js
@@ -1,4 +1,4 @@
-import React, { useEffect, useMemo } from "react";
+import React, { useEffect, useMemo, useState } from "react";
import { Button, Flex, Text } from "rebass";
import * as Icon from "../components/icons";
import { useStore as useUserStore } from "../stores/user-store";
@@ -15,6 +15,8 @@ import { usePersistentState } from "../utils/hooks";
import dayjs from "dayjs";
import { showRecoveryKeyDialog } from "../components/dialogs/recoverykeydialog";
import { showBuyDialog } from "../components/dialogs/buy-dialog";
+import Vault from "../common/vault";
+import { showToast } from "../utils/toast";
function importBackup() {
return new Promise((resolve, reject) => {
@@ -67,6 +69,7 @@ function Settings(props) {
"backupReminderOffset",
0
);
+ const [isVaultCreated, setIsVaultCreated] = useState(false);
const subscriptionDaysRemaining = useMemo(
() => dayjs(user?.subscription?.expiry).diff(dayjs(), "day") + 1,
@@ -82,6 +85,12 @@ function Settings(props) {
setTheme(isSystemThemeDark ? "dark" : "light");
}, [followSystemTheme, isSystemThemeDark, setTheme]);
+ useEffect(() => {
+ (async function () {
+ setIsVaultCreated(await db.vault.exists());
+ })();
+ }, [isVaultCreated]);
+
return (
<Flex variant="columnFill" px={2} sx={{ overflowY: "auto" }}>
{isLoggedIn ? (
@@ -281,6 +290,31 @@ function Settings(props) {
onSelectionChanged={(_option, index) => setBackupReminderOffset(index)}
/>
+ <Text
+ variant="subtitle"
+ color="primary"
+ sx={{ py: 1, borderBottom: "1px solid", borderBottomColor: "border" }}
+ >
+ Vault
+ </Text>
+
+ {!isVaultCreated && (
+ <Button variant="list" onClick={Vault.createVault}>
+ <TextWithTip
+ text="Create vault"
+ tip="Create a password-encrypted vault for your notes"
+ />
+ </Button>
+ )}
+
+ {isVaultCreated && (
+ <>
+ <Button variant="list" onClick={Vault.changeVaultPassword}>
+ <TextWithTip text="Change vault password" />
+ </Button>
+ </>
+ )}
+
<Text
variant="subtitle"
color="primary"