Skip to content

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

feat: replace session expired dialog with a full view

thecodrr4791300d3211
diff --git a/apps/web/package.json b/apps/web/package.json
index 91c2005f1..73c335c6e 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -11,7 +11,7 @@
"@mdi/react": "^1.4.0",
"@notesnook/desktop": "file:desktop",
"@rebass/forms": "^4.0.6",
- "@streetwritersco/tinymce-plugins": "^1.4.7",
+ "@streetwritersco/tinymce-plugins": "^1.4.8",
"@tinymce/tinymce-react": "^3.13.0",
"async-mutex": "^0.3.2",
"axios": "^0.21.4",
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index e10a0b83c..9a2a7b056 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -520,15 +520,6 @@ export function showRenameColorDialog(colorId) {
));
}
-export function showSessionExpiredDialog(email) {
- return showDialog((Dialogs, perform) => (
- <Dialogs.SessionExpiredDialog
- email={email}
- onClose={(res) => perform(res)}
- />
- ));
-}
-
export function showFeatureDialog(featureName) {
return showDialog((Dialogs, perform) => (
<Dialogs.FeatureDialog
diff --git a/apps/web/src/components/dialogs/index.js b/apps/web/src/components/dialogs/index.js
index 2be9599db..198b2ef1d 100644
--- a/apps/web/src/components/dialogs/index.js
+++ b/apps/web/src/components/dialogs/index.js
@@ -9,7 +9,6 @@ import MoveDialog from "./movenotedialog";
import PasswordDialog from "./passworddialog";
import RecoveryKeyDialog from "./recoverykeydialog";
import ItemDialog from "./itemdialog";
-import SessionExpiredDialog from "./sessionexpireddialog";
import FeatureDialog from "./feature-dialog";
import TrackingDetailsDialog from "./trackingdetailsdialog";
import ReminderDialog from "./reminderdialog";
@@ -28,7 +27,6 @@ const Dialogs = {
PasswordDialog,
RecoveryKeyDialog,
ItemDialog,
- SessionExpiredDialog,
FeatureDialog,
ProgressDialog,
ReminderDialog,
diff --git a/apps/web/src/components/dialogs/sessionexpireddialog.js b/apps/web/src/components/dialogs/sessionexpireddialog.js
deleted file mode 100644
index 7de7e3ce3..000000000
--- a/apps/web/src/components/dialogs/sessionexpireddialog.js
+++ /dev/null
@@ -1,142 +0,0 @@
-import { useEffect, useState } from "react";
-import { Flex, 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";
-import useDatabase from "../../hooks/use-database";
-
-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(true);
-
- const { onClose, email } = props;
- const [error, setError] = useState();
- const isLoggingIn = useStore((store) => store.isLoggingIn);
- const login = useStore((store) => store.login);
- const [isAppLoaded] = useDatabase();
-
- useEffect(() => {
- if (!isAppLoaded) return;
- setIsLoading(false);
- }, [onClose, isAppLoaded]);
-
- 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 this dialog, you will be logged out.</b>
- </Text>
- </Flex>
- }
- icon={Icon.Login}
- negativeButton={{
- text: "Sign out",
- disabled: isLoggingIn,
- onClick: () => {
- if (
- window.confirm(
- "Are you sure you want to sign out? This will clear all local data."
- )
- ) {
- onClose(false);
- }
- },
- }}
- positiveButton={{
- props: {
- form: "loginForm",
- type: "submit",
- },
- text: "Refresh session",
- loading: isLoggingIn || isLoading,
- disabled: isLoggingIn || isLoading,
- }}
- >
- {isAppLoaded ? (
- <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
- id="disabled"
- label="Email"
- name="disabled"
- autoComplete="disabled"
- defaultValue={maskEmail(email)}
- disabled={!!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>
- ) : null}
- </Dialog>
- );
-}
-export default SessionExpiredDialog;
diff --git a/apps/web/src/components/field/index.js b/apps/web/src/components/field/index.js
index b5bd59a69..f5cac9e00 100644
--- a/apps/web/src/components/field/index.js
+++ b/apps/web/src/components/field/index.js
@@ -123,6 +123,7 @@ function Field(props) {
<Flex
onClick={() => {
const input = document.getElementById(id);
+ if (!input) return;
input.type = isPasswordVisible ? "password" : "text";
setIsPasswordVisible((s) => !s);
}}
diff --git a/apps/web/src/index.js b/apps/web/src/index.js
index 335f00e1f..5cbb12525 100644
--- a/apps/web/src/index.js
+++ b/apps/web/src/index.js
@@ -1,6 +1,7 @@
import { render } from "react-dom";
import { getCurrentHash, getCurrentPath, makeURL } from "./navigation";
import * as serviceWorker from "./serviceWorkerRegistration";
+import Config from "./utils/config";
import { isTesting } from "./utils/platform";
if (process.env.REACT_APP_PLATFORM === "desktop") require("./commands");
@@ -17,6 +18,10 @@ const ROUTES = {
component: () => import("./views/auth"),
props: { type: "signup" },
},
+ "/sessionexpired": {
+ component: () => import("./views/auth"),
+ props: { type: "sessionexpired" },
+ },
"/login": {
component: () => import("./views/auth"),
props: { type: "login" },
@@ -29,6 +34,16 @@ const ROUTES = {
};
function getRoute() {
+ const isSessionExpired = Config.get("sessionExpired", false);
+ if (isSessionExpired) {
+ window.history.replaceState(
+ {},
+ null,
+ makeURL("/sessionexpired", getCurrentHash())
+ );
+ return ROUTES["/sessionexpired"];
+ }
+
const path = getCurrentPath();
if (!isTesting() && !shouldSkipInitiation() && !ROUTES[path]) {
window.history.replaceState({}, null, makeURL("/signup", getCurrentHash()));
diff --git a/apps/web/src/stores/user-store.js b/apps/web/src/stores/user-store.js
index adff073e4..37e547e67 100644
--- a/apps/web/src/stores/user-store.js
+++ b/apps/web/src/stores/user-store.js
@@ -4,7 +4,6 @@ import { store as appStore } from "./app-store";
import BaseStore from "./index";
import config from "../utils/config";
import { EV, EVENTS } from "notes-core/common";
-import { showSessionExpiredDialog } from "../common/dialog-controller";
import { showAccountLoggedOutNotice } from "../common/dialog-controller";
import Config from "../utils/config";
import { onPageVisibilityChanged } from "../utils/page-visibility";
@@ -20,15 +19,8 @@ class UserStore extends BaseStore {
EV.subscribe(EVENTS.appRefreshRequested, () => appStore.refresh());
EV.subscribe(EVENTS.userSessionExpired, async () => {
- const user = this.get().user;
- Config.set("sessionExpired", "true");
- const loginResult = await showSessionExpiredDialog(user.email);
- Config.set("sessionExpired", "false");
- if (!loginResult) {
- await db.user.logout(false);
- } else {
- window.location.reload();
- }
+ Config.set("sessionExpired", true);
+ window.location.replace("/sessionexpired");
});
db.user.getUser().then(async (user) => {
@@ -37,11 +29,10 @@ class UserStore extends BaseStore {
state.user = user;
state.isLoggedIn = true;
});
- if (Config.get("sessionExpired") === "true")
- EV.publish(EVENTS.userSessionExpired);
+ if (Config.get("sessionExpired")) EV.publish(EVENTS.userSessionExpired);
});
- if (Config.get("sessionExpired") === "true") return;
+ if (Config.get("sessionExpired")) return;
return db.user.fetchUser().then(async (user) => {
if (!user) return false;
diff --git a/apps/web/src/views/auth.js b/apps/web/src/views/auth.js
index fee84f648..14a7b9401 100644
--- a/apps/web/src/views/auth.js
+++ b/apps/web/src/views/auth.js
@@ -12,8 +12,78 @@ import Loader from "../components/loader";
import Logo from "../assets/logo.svg";
import LogoDark from "../assets/logo-dark.svg";
import { useStore as useThemeStore } from "../stores/theme-store";
+import {
+ showLoadingDialog,
+ showLogoutConfirmation,
+} from "../common/dialog-controller";
+import { showToast } from "../utils/toast";
const authTypes = {
+ sessionexpired: {
+ title: "Your session has expired",
+ subtitle: {
+ text: (
+ <Flex bg="shade" p={1} sx={{ borderRadius: "default" }}>
+ <Text as="span" fontSize="body" color="primary">
+ <b>
+ All your local changes are safe and will be synced after you
+ relogin.
+ </b>{" "}
+ Please enter your password to continue.
+ </Text>
+ </Flex>
+ ),
+ },
+ fields: [
+ {
+ id: "email",
+ name: "email",
+ label: "Your account email",
+ defaultValue: (user) => maskEmail(user?.email),
+ disabled: true,
+ autoComplete: "false",
+ type: "email",
+ },
+ {
+ id: "password",
+ name: "password",
+ label: "Enter your password",
+ autoComplete: "current-password",
+ type: "password",
+ autoFocus: true,
+ },
+ ],
+ primaryAction: {
+ text: "Relogin to your account",
+ },
+ secondaryAction: {
+ text: <Text color="error">Logout permanently</Text>,
+ onClick: async () => {
+ if (await showLogoutConfirmation()) {
+ await showLoadingDialog({
+ title: "You are being logged out",
+ action: () => db.user.logout(true),
+ });
+ showToast("success", "You have been logged out.");
+ Config.set("sessionExpired", false);
+ window.location.replace("/login");
+ }
+ },
+ },
+ loading: {
+ title: "Logging you in",
+ text: "Please wait while you are authenticated.",
+ },
+ onSubmit: async (form, onError) => {
+ return await userstore
+ .login(form)
+ .then(async () => {
+ Config.set("sessionExpired", false);
+ redirectToURL(form.redirect || "/");
+ })
+ .catch((e) => onError(e.message));
+ },
+ },
signup: {
title: "Create an account",
subtitle: {
@@ -23,9 +93,30 @@ const authTypes = {
onClick: () => hardNavigate("/login", getQueryParams()),
},
},
- labels: { email: "Enter email", password: "Set password" },
- confirmPassword: true,
- autoComplete: { password: "new-password" },
+ fields: [
+ {
+ id: "email",
+ name: "email",
+ label: "Enter email",
+ autoComplete: "email",
+ type: "email",
+ autoFocus: true,
+ },
+ {
+ id: "password",
+ name: "password",
+ label: "Set password",
+ autoComplete: "new-password",
+ type: "password",
+ },
+ {
+ id: "confirm-password",
+ name: "confirmPassword",
+ label: "Confirm password",
+ autoComplete: "confirm-password",
+ type: "password",
+ },
+ ],
primaryAction: {
text: "Create account",
},
@@ -39,7 +130,7 @@ const authTypes = {
title: "Creating your account",
text: "Please wait while we finalize your account.",
},
- agreementText: (
+ footer: (
<>
By pressing "Create account" button, you agree to our{" "}
<Text
@@ -85,11 +176,23 @@ const authTypes = {
onClick: () => hardNavigate("/signup", getQueryParams()),
},
},
- autoComplete: { password: "current-password" },
- labels: {
- email: "Enter email",
- password: "Enter password",
- },
+ fields: [
+ {
+ type: "email",
+ id: "email",
+ name: "email",
+ label: "Enter email",
+ autoComplete: "email",
+ autoFocus: true,
+ },
+ {
+ type: "password",
+ id: "password",
+ name: "password",
+ label: "Enter password",
+ autoComplete: "current-password",
+ },
+ ],
primaryAction: {
text: "Login to your account",
},
@@ -145,7 +248,7 @@ function Auth(props) {
const [success, setSuccess] = useState();
const [isAppLoaded] = useDatabase();
const [form, setForm] = useState({});
-
+ const [user, setUser] = useState();
const theme = useThemeStore((store) => store.theme);
const data = authTypes[type];
@@ -161,9 +264,15 @@ function Auth(props) {
if (!isAppLoaded) return;
(async () => {
const user = await db.user.getUser();
- if (!!user) redirectToURL("/");
+ const isSessionExpired = Config.get("sessionExpired", false);
+ if (user) {
+ if (type === "sessionexpired" && isSessionExpired) setUser(user);
+ else redirectToURL("/");
+ } else if (type === "sessionexpired") {
+ redirectToURL("/");
+ }
})();
- }, [isAppLoaded]);
+ }, [isAppLoaded, type]);
return (
<ThemeProvider>
@@ -225,11 +334,13 @@ function Auth(props) {
boxShadow: "0px 0px 60px 10px #00000022",
}}
onSubmit={async (e) => {
+ console.log(e);
e.preventDefault();
setIsSubmitting(true);
const formData = new FormData(e.target);
const form = Object.fromEntries(formData.entries());
form.redirect = redirect;
+ if (user) form.email = user.email;
setForm(form);
await data.onSubmit(
form,
@@ -241,11 +352,7 @@ function Auth(props) {
);
}}
>
- {!isAppLoaded ? (
- <>
- <Loading />
- </>
- ) : isSubmitting ? (
+ {isSubmitting ? (
<>
<Loader title={data.loading.title} text={data.loading.text} />
</>
@@ -264,6 +371,7 @@ function Auth(props) {
<Text
variant="body"
mt={1}
+ mb={2}
textAlign="center"
color="fontTertiary"
>
@@ -282,21 +390,22 @@ function Auth(props) {
</Text>
)}
</Text>
- <Field
- styles={{
- container: { mt: 4 },
- }}
- autoFocus
- data-test-id="email"
- id="email"
- required
- name="email"
- autoComplete="email"
- label={data.labels.email}
- helpText={data.helpTexts?.email}
- defaultValue={form.email}
- />
- {data.labels.password && (
+ {data.fields?.map(({ defaultValue, id, autoFocus, ...rest }) => (
+ <Field
+ {...rest}
+ id={id}
+ key={id}
+ required
+ styles={{
+ container: { mt: 2 },
+ }}
+ data-test-id={id}
+ autoFocus={autoFocus}
+ defaultValue={defaultValue ? defaultValue(user) : form.email}
+ />
+ ))}
+
+ {/* {data.labels.password && (
<Field
styles={{
container: { mt: 2 },
@@ -326,7 +435,7 @@ function Auth(props) {
type="password"
defaultValue={form.confirmPassword}
/>
- )}
+ )} */}
{/* {data.supportsPasswordRecovery && (
<Button
@@ -348,8 +457,13 @@ function Auth(props) {
sx={{ borderRadius: "default" }}
justifyContent="center"
alignItems="center"
+ disabled={!isAppLoaded}
>
- {data.primaryAction.text}
+ {isAppLoaded ? (
+ data.primaryAction.text
+ ) : (
+ <Loading color="static" />
+ )}
</Button>
{data.secondaryAction && (
<>
@@ -385,9 +499,9 @@ function Auth(props) {
</Text>
</Flex>
)}
- {data.agreementText && (
+ {data.footer && (
<Text mt={2} variant="subBody" textAlign="center">
- {data.agreementText}
+ {data.footer}
</Text>
)}
</>
@@ -403,3 +517,14 @@ function redirectToURL(url) {
Config.set("skipInitiation", true);
hardNavigate(url);
}
+
+function maskEmail(email) {
+ if (!email) return "";
+ 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}`;
+}
diff --git a/apps/web/src/views/settings.js b/apps/web/src/views/settings.js
index b6f132bd2..88aeaf1ae 100644
--- a/apps/web/src/views/settings.js
+++ b/apps/web/src/views/settings.js
@@ -253,8 +253,6 @@ function Settings(props) {
if (await showLogoutConfirmation()) {
await showLoadingDialog({
title: "You are being logged out",
- subtitle:
- "Please do NOT close your browser or shut down your PC.",
action: () => db.user.logout(true),
});
showToast("success", "You have been logged out.");