Skip to content

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

fix: recheck access_token on session expiry

thecodrr8e14dd1293fb
diff --git a/apps/web/src/components/dialogs/sessionexpireddialog.js b/apps/web/src/components/dialogs/sessionexpireddialog.js
index 5e6a8972f..989276f34 100644
--- a/apps/web/src/components/dialogs/sessionexpireddialog.js
+++ b/apps/web/src/components/dialogs/sessionexpireddialog.js
@@ -1,11 +1,12 @@
-import React, { useState } from "react";
-import { Flex, Button, Text } from "rebass";
+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 { showToast } from "../../utils/toast";
+import useDatabase from "../../hooks/use-database";
const requiredValues = ["email", "password"];
function maskEmail(email) {
@@ -25,6 +26,22 @@ function SessionExpiredDialog(props) {
const [error, setError] = useState();
const isLoggingIn = useStore((store) => store.isLoggingIn);
const login = useStore((store) => store.login);
+ const [isAppLoaded] = useDatabase();
+
+ useEffect(() => {
+ if (!isAppLoaded) return;
+
+ (async () => {
+ setIsLoading(true);
+ try {
+ const token = await db.user.tokenManager.getToken();
+ if (token) onClose(true);
+ } catch (e) {
+ } finally {
+ setIsLoading(false);
+ }
+ })();
+ }, [onClose, isAppLoaded]);
return (
<Dialog
@@ -34,19 +51,19 @@ function SessionExpiredDialog(props) {
<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>
+ <b>If you close this dialog, you will be logged out.</b>
</Text>
</Flex>
}
icon={Icon.Login}
scrollable
negativeButton={{
- text: "Sign out & erase all data",
+ text: "Sign out",
disabled: isLoggingIn,
onClick: () => {
if (
window.confirm(
- "Are you sure you want to sign out and erase all data?"
+ "Are you sure you want to sign out? This will clear all local data."
)
) {
onClose(false);
@@ -63,72 +80,73 @@ function SessionExpiredDialog(props) {
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();
+ {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;
+ 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();
+ login(data, true)
+ .then(async () => {
+ onClose(true);
})
- .catch((e) => setError(e.message))
- .finally(() => setIsLoading(false));
+ .catch((e) => setError(e.message));
}}
+ flexDirection="column"
>
- Forgot password?
- </Button>
- {error && <Text variant="error">{error}</Text>}
- </Flex>
+ <Field
+ required
+ id="email"
+ label="Email"
+ name="email-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>
);
}