Skip to content

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

feat: add proper session expired dialog

thecodrr9cc7c4ecd692
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index 3db768b75..ad979fc47 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -502,3 +502,12 @@ export function showEditTopicDialog(notebookId, topicId) {
/>
));
}
+
+export function showSessionExpiredDialog(email) {
+ return showDialog((Dialogs, perform) => (
+ <Dialogs.SessionExpiredDialog
+ email={email}
+ onClose={(res) => perform(res)}
+ />
+ ));
+}
diff --git a/apps/web/src/components/dialogs/index.js b/apps/web/src/components/dialogs/index.js
index ff3024973..4e07130fc 100644
--- a/apps/web/src/components/dialogs/index.js
+++ b/apps/web/src/components/dialogs/index.js
@@ -11,6 +11,7 @@ import PasswordDialog from "./passworddialog";
import RecoveryKeyDialog from "./recoverykeydialog";
import SignUpDialog from "./signupdialog";
import TopicDialog from "./topicdialog";
+import SessionExpiredDialog from "./sessionexpireddialog";
const Dialogs = {
AddNotebookDialog,
@@ -26,5 +27,6 @@ const Dialogs = {
RecoveryKeyDialog,
SignUpDialog,
TopicDialog,
+ SessionExpiredDialog,
};
export default Dialogs;
diff --git a/apps/web/src/components/dialogs/sessionexpireddialog.js b/apps/web/src/components/dialogs/sessionexpireddialog.js
new file mode 100644
index 000000000..5e6a8972f
--- /dev/null
+++ b/apps/web/src/components/dialogs/sessionexpireddialog.js
@@ -0,0 +1,135 @@
+import React, { useState } from "react";
+import { Flex, Button, Text } from "rebass";
+import * as Icon from "../icons";
+import Dialog from "./dialog";
+import { useStore } from "../../stores/user-store";
+import Field from "../field";
+import { db } from "../../common/db";
+import { showToast } from "../../utils/toast";
+
+const requiredValues = ["email", "password"];
+function maskEmail(email) {
+ const [username, domain] = email.split("@");
+ const maskChars = "*".repeat(
+ username.substring(2, username.length - 2).length
+ );
+ return `${username.substring(0, 2)}${maskChars}${username.substring(
+ username.length - 2
+ )}@${domain}`;
+}
+
+function SessionExpiredDialog(props) {
+ const [isLoading, setIsLoading] = useState(false);
+
+ const { onClose, email } = props;
+ const [error, setError] = useState();
+ const isLoggingIn = useStore((store) => store.isLoggingIn);
+ const login = useStore((store) => store.login);
+
+ return (
+ <Dialog
+ isOpen={true}
+ title={"Session expired"}
+ description={
+ <Flex bg="errorBg" p={1} sx={{ borderRadius: "default" }}>
+ <Text as="span" fontSize="body" color="error">
+ Please sign in again to continue.{" "}
+ <b>If you close the dialog, all your data will be erased.</b>
+ </Text>
+ </Flex>
+ }
+ icon={Icon.Login}
+ scrollable
+ negativeButton={{
+ text: "Sign out & erase all data",
+ disabled: isLoggingIn,
+ onClick: () => {
+ if (
+ window.confirm(
+ "Are you sure you want to sign out and erase all data?"
+ )
+ ) {
+ onClose(false);
+ }
+ },
+ }}
+ positiveButton={{
+ props: {
+ form: "loginForm",
+ type: "submit",
+ },
+ text: "Sign in",
+ loading: isLoggingIn || isLoading,
+ disabled: isLoggingIn || isLoading,
+ }}
+ >
+ <Flex
+ id="loginForm"
+ as="form"
+ onSubmit={(e) => {
+ e.preventDefault();
+ const form = new FormData(e.target);
+ const data = requiredValues.reduce((prev, curr) => {
+ prev[curr] = form.get(curr);
+ return prev;
+ }, {});
+ setError();
+
+ if (email) data.email = email;
+
+ login(data, true)
+ .then(async () => {
+ onClose(true);
+ })
+ .catch((e) => setError(e.message));
+ }}
+ flexDirection="column"
+ >
+ <Field
+ required
+ id="email"
+ label="Email"
+ name="email"
+ defaultValue={maskEmail(email)}
+ disabled={!!email}
+ autoComplete="email"
+ />
+ <Field
+ required
+ id="password"
+ label="Password"
+ name="password"
+ type="password"
+ autoComplete="current-password"
+ sx={{ mt: 1 }}
+ />
+ <Button
+ type="button"
+ variant="anchor"
+ fontSize="body"
+ sx={{ alignSelf: "flex-start" }}
+ mt={1}
+ onClick={() => {
+ setError();
+ setIsLoading(true);
+ db.user
+ .recoverAccount(email.toLowerCase())
+ .then(() => {
+ showToast(
+ "success",
+ "Recovery email sent. Please check your inbox."
+ );
+ onClose();
+ })
+ .catch((e) => setError(e.message))
+ .finally(() => setIsLoading(false));
+ }}
+ >
+ Forgot password?
+ </Button>
+ {error && <Text variant="error">{error}</Text>}
+ </Flex>
+ </Dialog>
+ );
+}
+export default SessionExpiredDialog;
diff --git a/apps/web/src/components/field/index.js b/apps/web/src/components/field/index.js
index 26f1bc7fd..88297bc0d 100644
--- a/apps/web/src/components/field/index.js
+++ b/apps/web/src/components/field/index.js
@@ -81,6 +81,11 @@ function Field(props) {
placeholder={placeholder}
autoComplete={autoComplete}
type={type || "text"}
+ sx={{
+ ":disabled": {
+ bg: "bgSecondary",
+ },
+ }}
onChange={(e) => {
if (validatePassword) {
const value = e.target.value;
diff --git a/apps/web/src/stores/user-store.js b/apps/web/src/stores/user-store.js
index 805e1a8bf..938c1cc33 100644
--- a/apps/web/src/stores/user-store.js
+++ b/apps/web/src/stores/user-store.js
@@ -5,7 +5,10 @@ import { store as appStore } from "./app-store";
import BaseStore from "./index";
import config from "../utils/config";
import { EV, EVENTS } from "notes-core/common";
-import { showLoadingDialog, showLogInDialog } from "../common/dialog-controller";
+import {
+ showLoadingDialog,
+ showSessionExpiredDialog,
+} from "../common/dialog-controller";
import { Text } from "rebass";
import { showToast } from "../utils/toast";
import { showAccountLoggedOutNotice } from "../common/dialog-controller";
@@ -26,14 +29,7 @@ class UserStore extends BaseStore {
EV.subscribe(EVENTS.userSessionExpired, async () => {
const user = this.get().user;
Config.set("sessionExpired", "true");
- const loginResult = await showLogInDialog(
- "Your session has expired.",
- "Please login again to continue. If you press Cancel or close the dialog, all your data will be erased.",
- "Login",
- user.email,
- true,
- true
- );
+ const loginResult = await showSessionExpiredDialog(user.email);
Config.set("sessionExpired", "false");
if (!loginResult) {
await db.user.logout(false);
@@ -142,6 +138,7 @@ class UserStore extends BaseStore {
return await appStore.refresh();
})
.catch(async (err) => {
+ console.error(err);
if (err.code === "MERGE_CONFLICT") await appStore.refresh();
else {
showToast("error", err.message);