Skip to content

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

feat: fully implement login/signup

thecodrr81009bacce5f
diff --git a/apps/web/src/app.js b/apps/web/src/app.js
index dbede7425..d84d3225c 100644
--- a/apps/web/src/app.js
+++ b/apps/web/src/app.js
@@ -17,6 +17,7 @@ import { COLORS } from "./common";
import { toTitleCase } from "./utils/string";
import * as Icon from "./components/icons";
import { useStore as useAppStore } from "./stores/app-store";
+import { useStore as useUserStore } from "./stores/user-store";
import Animated from "./components/animated";
const NavMenuItem = props => {
@@ -78,10 +79,12 @@ function App() {
const refreshColors = useStore(store => store.refreshColors);
const setSelectedContext = useNotesStore(store => store.setSelectedContext);
const isFocusModeEnabled = useAppStore(store => store.isFocusModeEnabled);
+ const initUser = useUserStore(store => store.init);
useEffect(() => {
RootNavigator.navigate(selectedKey);
refreshColors();
+ initUser();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
@@ -95,6 +98,7 @@ function App() {
}, [isFocusModeEnabled]);
const colors = useStore(store => store.colors);
+ const isLoggedIn = useUserStore(store => store.isLoggedIn);
return (
<ThemeProvider>
<Flex
@@ -185,7 +189,9 @@ function App() {
<NavMenuItem
onSelected={async () => {
if (item.onClick) {
- return item.onClick();
+ item.onClick();
+ if (item.component) setSelectedKey(item.key);
+ return;
}
if (RootNavigator.navigate(item.key)) {
setSelectedKey(item.key);
diff --git a/apps/web/src/components/dialogs/logindialog.js b/apps/web/src/components/dialogs/logindialog.js
index 08156e499..3f4c78bb4 100644
--- a/apps/web/src/components/dialogs/logindialog.js
+++ b/apps/web/src/components/dialogs/logindialog.js
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import { Flex, Box, Button, Text } from "rebass";
import { Input } from "@rebass/forms";
-import * as Icon from "react-feather";
+import * as Icon from "../icons";
import Dialog, { showDialog } from "./dialog";
import { showSignUpDialog } from "./signupdialog";
import { db } from "../../common";
@@ -15,7 +15,7 @@ const LoginDialog = props => {
<Dialog
isOpen={true}
title={"Login"}
- icon={Icon.LogIn}
+ icon={Icon.Login}
onCloseClick={props.onClose}
negativeButton={{ onClick: props.onClose }}
positiveButton={{
@@ -32,7 +32,14 @@ const LoginDialog = props => {
return;
}
- db.user.login(username, password);
+ db.user
+ .login(username, password)
+ .then(() => {
+ props.onClose();
+ })
+ .catch(() => {
+ setErrorMessage("Failed to login. Please try again.");
+ });
}
}}
content={
@@ -87,5 +94,5 @@ const LoginDialog = props => {
};
export const showLogInDialog = () => {
- return showDialog(perform => <LoginDialog onClose={() => perform(false)} />);
+ return showDialog(perform => <LoginDialog onClose={() => perform()} />);
};
diff --git a/apps/web/src/components/dialogs/signupdialog.js b/apps/web/src/components/dialogs/signupdialog.js
index 1c4de0ee2..5fb50c60f 100644
--- a/apps/web/src/components/dialogs/signupdialog.js
+++ b/apps/web/src/components/dialogs/signupdialog.js
@@ -43,7 +43,14 @@ const SignUpDialog = props => {
return;
}
- db.user.signup(username, email, password);
+ db.user
+ .signup(username, email, password)
+ .then(() => {
+ props.onClose();
+ })
+ .catch(() => {
+ setErrorMessage("Couldn't signup. Please try again.");
+ });
}
}}
content={
@@ -58,6 +65,7 @@ const SignUpDialog = props => {
<Input
variant="default"
placeholder="Email"
+ sx={{ marginTop: 2 }}
onChange={e => {
setEmail(e.target.value);
}}
@@ -100,5 +108,5 @@ const SignUpDialog = props => {
};
export const showSignUpDialog = () => {
- return showDialog(perform => <SignUpDialog onClose={() => perform(false)} />);
+ return showDialog(perform => <SignUpDialog onClose={() => perform()} />);
};
diff --git a/apps/web/src/navigation/navigators/rootnavigator.js b/apps/web/src/navigation/navigators/rootnavigator.js
index c8b199357..678c412fa 100644
--- a/apps/web/src/navigation/navigators/rootnavigator.js
+++ b/apps/web/src/navigation/navigators/rootnavigator.js
@@ -4,30 +4,35 @@ import {
Trash,
NotebooksContainer,
TagsContainer,
- Notes
+ Notes,
+ Account
} from "../../views";
import * as Icon from "../../components/icons";
import { createRoute, createNormalRoute, createDeadRoute } from "../routes";
import Navigator from "../index";
-import { showLogInDialog } from "../../components/dialogs/logindialog";
import { changeTheme, isDarkTheme } from "../../utils/theme";
import SelectionModeOptions from "../../common/selectionoptions";
import Search from "../../views/Search";
-import { store } from "../../stores/note-store";
+import { store as noteStore } from "../../stores/note-store";
+import { store as userStore } from "../../stores/user-store";
+import { showLogInDialog } from "../../components/dialogs/logindialog";
export const bottomRoutes = {
...createDeadRoute("nightmode", Icon.Theme, {
onClick: () => changeTheme(),
- bottom: true,
isToggled: () => isDarkTheme()
}),
- ...createDeadRoute("signin", Icon.Login, {
- onClick: () => showLogInDialog(),
- bottom: true
+ ...createNormalRoute("account", Account, Icon.User, {
+ onClick: async () => {
+ if (userStore.getState().isLoggedIn) {
+ RootNavigator.navigate("account");
+ } else {
+ await showLogInDialog();
+ }
+ }
}),
...createRoute("settings", SettingsContainer, {
- icon: Icon.Settings,
- bottom: true
+ icon: Icon.Settings
})
};
@@ -43,7 +48,7 @@ export const routes = {
title: "Favorites",
options: SelectionModeOptions.FavoritesOptions,
onClick: () => {
- store.getState().setSelectedContext({
+ noteStore.getState().setSelectedContext({
type: "favorites"
});
RootNavigator.navigate("favorites");
diff --git a/apps/web/src/navigation/navigators/settingnavigator.js b/apps/web/src/navigation/navigators/settingnavigator.js
index 63206894d..f3d52c5f8 100644
--- a/apps/web/src/navigation/navigators/settingnavigator.js
+++ b/apps/web/src/navigation/navigators/settingnavigator.js
@@ -4,7 +4,7 @@ import { createRoute } from "../routes";
const routes = {
...createRoute("settings", Settings, { title: "Settings" }),
- ...createRoute("account", Account),
+ ...createRoute("account", Account, { title: "Account" }),
...createRoute("general", General),
...createRoute("TOS", TOS),
...createRoute("about", TOS),
diff --git a/apps/web/src/stores/user-store.js b/apps/web/src/stores/user-store.js
new file mode 100644
index 000000000..047b3f175
--- /dev/null
+++ b/apps/web/src/stores/user-store.js
@@ -0,0 +1,41 @@
+import createStore from "../common/store";
+import { db } from "../common";
+
+function userStore(set, get) {
+ return {
+ isLoggedIn: false,
+ user: undefined,
+ init: () => {
+ return db.user.get().then(user => {
+ if (!user) return false;
+ set(state => {
+ state.user = user;
+ state.isLoggedIn = true;
+ });
+ return true;
+ });
+ },
+ login: (username, password) => {
+ return db.user
+ .login(username, password)
+ .then(() => {
+ return get().init();
+ })
+ .catch(() => {
+ return false;
+ });
+ },
+ logout: () => {
+ db.user.logout().then(() => {
+ set(state => {
+ state.user = undefined;
+ state.isLoggedIn = false;
+ });
+ });
+ }
+ };
+}
+
+const [useStore, store] = createStore(userStore);
+
+export { useStore, store };
diff --git a/apps/web/src/views/Settings.js b/apps/web/src/views/Settings.js
index 5e3b66e3b..bb9930dfb 100644
--- a/apps/web/src/views/Settings.js
+++ b/apps/web/src/views/Settings.js
@@ -1,14 +1,16 @@
import React, { useState, useEffect } from "react";
-import { Box, Button, Flex, Text, Image } from "rebass";
+import { Box, Button, Flex, Text } from "rebass";
import * as Icon from "../components/icons";
-import { Switch } from "@rebass/forms";
import "../app.css";
import { changeTheme, isDarkTheme, changeAccent } from "../utils/theme";
import { useTheme } from "emotion-theming";
+import { useStore as useUserStore } from "../stores/user-store";
const Settings = props => {
const [check, setCheck] = useState(isDarkTheme());
const theme = useTheme();
+ const user = useUserStore(store => store.user);
+ const isLoggedIn = useUserStore(store => store.isLoggedIn);
return (
<Flex flexDirection="column" flex="1 1 auto">
<Flex
@@ -16,7 +18,8 @@ const Settings = props => {
bg="shade"
mx={2}
p={2}
- sx={{ borderRadius: "default" }}
+ sx={{ borderRadius: "default", cursor: "pointer" }}
+ onClick={() => props.navigator.navigate("account")}
>
<Flex
flexDirection="column"
@@ -33,12 +36,21 @@ const Settings = props => {
<Icon.User color="static" />
</Flex>
<Flex flexDirection="column" justifyContent="center">
- <Flex fontSize="subBody" color="gray">
- You are not logged in
- </Flex>
- <Flex fontSize="body" color="primary">
- Login to sync notes.
- </Flex>
+ {isLoggedIn ? (
+ <>
+ <Text variant="title">{user.username}</Text>
+ <Text fontSize="subBody">{user.email}</Text>
+ </>
+ ) : (
+ <>
+ <Text fontSize="subBody" color="gray">
+ You are not logged in
+ </Text>
+ <Text fontSize="body" color="primary">
+ Login to sync notes.
+ </Text>
+ </>
+ )}
</Flex>
</Flex>
<Box