Skip to content

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

web: migrate to new 3-step login flow

Abdullah Atta3fd1898422d0
diff --git a/apps/web/src/index.tsx b/apps/web/src/index.tsx
index ce6f42883..2e55ca71c 100644
--- a/apps/web/src/index.tsx
+++ b/apps/web/src/index.tsx
@@ -76,19 +76,23 @@ const routes = {
},
"/login": {
component: () => import("./views/auth"),
- props: { route: "login" }
+ props: { route: "login:email" }
+ },
+ "/login/password": {
+ component: () => import("./views/auth"),
+ props: { route: "login:email" }
},
"/recover": {
component: () => import("./views/auth"),
props: { route: "recover" }
},
- "/mfa/code": {
+ "/login/mfa/code": {
component: () => import("./views/auth"),
- props: { route: "login" }
+ props: { route: "login:email" }
},
- "/mfa/select": {
+ "/login/mfa/select": {
component: () => import("./views/auth"),
- props: { route: "login" }
+ props: { route: "login:email" }
},
default: { component: () => import("./app"), props: null }
} as const;
@@ -97,8 +101,9 @@ const sessionExpiryExceptions: Routes[] = [
"/recover",
"/account/recovery",
"/sessionexpired",
- "/mfa/code",
- "/mfa/select"
+ "/login/mfa/code",
+ "/login/mfa/select",
+ "/login/password"
];
const serviceWorkerWhitelist: Routes[] = ["default"];
@@ -158,7 +163,7 @@ async function renderApp() {
const { default: Component } = await component();
render(
- <Component route={props?.route || "login"} />,
+ <Component route={props?.route || "login:email"} />,
document.getElementById("root"),
() => {
logger.measure("app render");
diff --git a/apps/web/src/stores/user-store.js b/apps/web/src/stores/user-store.js
index 6103c8fe4..db85d02ca 100644
--- a/apps/web/src/stores/user-store.js
+++ b/apps/web/src/stores/user-store.js
@@ -124,11 +124,16 @@ class UserStore extends BaseStore {
login = async (form, skipInit = false) => {
this.set((state) => (state.isLoggingIn = true));
- const { email, password, code, method } = form;
+ const { email, password, code, method, token } = form;
try {
- if (code) await db.user.mfaLogin(email, password, { code, method });
- else await db.user.login(email, password);
+ if (code) {
+ await db.user.authenticateMultiFactorCode(code, method, token);
+ } else if (password) {
+ await db.user.authenticatePassword(email, password, token, null);
+ } else if (email) {
+ await db.user.authenticateEmail(email);
+ }
if (skipInit) return true;
return this.init();
diff --git a/apps/web/src/views/auth.tsx b/apps/web/src/views/auth.tsx
index 78979d56a..1040036cf 100644
--- a/apps/web/src/views/auth.tsx
+++ b/apps/web/src/views/auth.tsx
@@ -49,25 +49,31 @@ import {
showLogoutConfirmation
} from "../common/dialog-controller";
-type LoginFormData = {
+type EmailFormData = {
email: string;
+};
+
+type PasswordFormData = EmailFormData & {
password: string;
+ token: string;
};
-type MFALoginFormData = LoginFormData & {
+type MFALoginFormData = EmailFormData & {
code?: string;
method?: MFAMethodType;
+ token: string;
};
-type SignupFormData = LoginFormData & {
- "confirm-password": string;
-};
+type SignupFormData = EmailFormData &
+ PasswordFormData & {
+ "confirm-password": string;
+ };
type AccountRecoveryFormData = {
email: string;
};
-type MFAFormData = LoginFormData & {
+type MFAFormData = EmailFormData & {
selectedMethod: MFAMethodType;
primaryMethod: MFAMethodType;
code?: string;
@@ -83,10 +89,15 @@ type MFAErrorData = {
phoneNumber?: string;
};
+type PasswordRequiredErrorData = {
+ token: string;
+};
+
type AuthFormData = {
- login: LoginFormData;
+ "login:email": EmailFormData;
+ "login:password": PasswordFormData;
signup: SignupFormData;
- sessionExpiry: LoginFormData;
+ sessionExpiry: EmailFormData & PasswordFormData;
recover: AccountRecoveryFormData;
"mfa:code": MFAFormData;
"mfa:select": MFAFormData;
@@ -94,7 +105,8 @@ type AuthFormData = {
type BaseFormData =
| MFAFormData
- | LoginFormData
+ | EmailFormData
+ | PasswordFormData
| AccountRecoveryFormData
| SignupFormData;
@@ -108,7 +120,8 @@ type BaseAuthComponentProps<TRoute extends AuthRoutes> = {
};
type AuthRoutes =
| "sessionExpiry"
- | "login"
+ | "login:email"
+ | "login:password"
| "signup"
| "recover"
| "mfa:code"
@@ -123,8 +136,10 @@ function getRouteComponent<TRoute extends AuthRoutes>(
route: TRoute
): AuthComponent<TRoute> | undefined {
switch (route) {
- case "login":
- return Login as AuthComponent<TRoute>;
+ case "login:email":
+ return LoginEmail as AuthComponent<TRoute>;
+ case "login:password":
+ return LoginPassword as AuthComponent<TRoute>;
case "signup":
return Signup as AuthComponent<TRoute>;
case "sessionExpiry":
@@ -140,16 +155,18 @@ function getRouteComponent<TRoute extends AuthRoutes>(
}
const routePaths: Record<AuthRoutes, string> = {
- login: "/login",
+ "login:email": "/login",
+ "login:password": "/login/password",
+ "mfa:code": "/login/mfa/code",
+ "mfa:select": "/login/mfa/select",
recover: "/recover",
sessionExpiry: "/sessionexpired",
- signup: "/signup",
- "mfa:code": "/mfa/code",
- "mfa:select": "/mfa/select"
+ signup: "/signup"
};
const authorizedRoutes: AuthRoutes[] = [
- "login",
+ "login:email",
+ "login:password",
"signup",
"mfa:code",
"mfa:select",
@@ -190,7 +207,9 @@ function Auth(props: AuthProps) {
flexDirection: "column"
}}
>
- {route === "login" || route === "signup" || route === "recover" ? (
+ {route === "login:email" ||
+ route === "signup" ||
+ route === "recover" ? (
<Button
sx={{
display: "flex",
@@ -252,12 +271,12 @@ function Auth(props: AuthProps) {
}
export default Auth;
-function Login(props: BaseAuthComponentProps<"login">) {
+function LoginEmail(props: BaseAuthComponentProps<"login:email">) {
const { navigate } = props;
return (
<AuthForm
- type="login"
+ type="login:email"
title="Welcome back!"
subtitle={
<SubtitleWithAction
@@ -271,16 +290,58 @@ function Login(props: BaseAuthComponentProps<"login">) {
}}
onSubmit={(form) => login(form, navigate)}
>
- {(form?: LoginFormData) => (
+ {(form?: EmailFormData) => (
<>
<AuthField
id="email"
type="email"
autoComplete="email"
label="Enter email"
- autoFocus={!form?.password}
+ autoFocus
defaultValue={form?.email}
/>
+ <SubmitButton text="Login to your account" />
+ </>
+ )}
+ </AuthForm>
+ );
+}
+
+function LoginPassword(props: BaseAuthComponentProps<"login:password">) {
+ const { navigate, formData } = props;
+
+ if (!formData) {
+ openURL("/");
+ return null;
+ }
+
+ return (
+ <AuthForm
+ type="login:password"
+ title="Welcome back!"
+ subtitle={
+ <SubtitleWithAction
+ text="Don't have an account?"
+ action={{ text: "Sign up", onClick: () => navigate("signup") }}
+ />
+ }
+ loading={{
+ title: "Logging you in",
+ subtitle: "Please wait while you are authenticated."
+ }}
+ onSubmit={(form) => {
+ return login(
+ {
+ password: form.password,
+ email: formData.email,
+ token: formData.token
+ },
+ navigate
+ );
+ }}
+ >
+ {(form?: PasswordFormData) => (
+ <>
<AuthField
id="password"
type="password"
@@ -315,7 +376,7 @@ function Signup(props: BaseAuthComponentProps<"signup">) {
subtitle={
<SubtitleWithAction
text="Already have an account?"
- action={{ text: "Log in", onClick: () => navigate("login") }}
+ action={{ text: "Log in", onClick: () => navigate("login:email") }}
/>
}
loading={{
@@ -467,7 +528,7 @@ function AccountRecovery(props: BaseAuthComponentProps<"recover">) {
subtitle={
<SubtitleWithAction
text="Remembered your password?"
- action={{ text: "Log in", onClick: () => navigate("login") }}
+ action={{ text: "Log in", onClick: () => navigate("login:email") }}
/>
}
loading={{
@@ -598,9 +659,11 @@ function MFACode(props: BaseAuthComponentProps<"mfa:code">) {
return null;
}
- const { selectedMethod, token } = formData;
+ const { selectedMethod } = formData;
const texts = getTexts(formData)[selectedMethod];
+ if (!texts) return null;
+
return (
<AuthForm
type="mfa:code"
@@ -612,10 +675,10 @@ function MFACode(props: BaseAuthComponentProps<"mfa:code">) {
}}
onSubmit={async (form) => {
const loginForm: MFALoginFormData = {
- email: formData.email,
- password: formData.password,
code: form.code,
- method: formData.selectedMethod
+ email: formData.email,
+ method: formData.selectedMethod,
+ token: formData.token
};
await login(loginForm, navigate);
}}
@@ -645,7 +708,7 @@ function MFACode(props: BaseAuthComponentProps<"mfa:code">) {
</Text>
),
onClick: async () => {
- await sendCode(selectedMethod, token);
+ await sendCode(selectedMethod, formData.token);
}
}
: undefined
@@ -941,7 +1004,7 @@ export function SubmitButton(props: SubmitButtonProps) {
}
async function login(
- form: LoginFormData | MFALoginFormData,
+ form: EmailFormData | PasswordFormData | MFALoginFormData,
navigate: NavigateFunction
) {
try {
@@ -949,6 +1012,7 @@ async function login(
Config.set("sessionExpired", false);
openURL("/");
} catch (e) {
+ console.error(e);
const error = e as Error & { code?: string; data?: unknown };
if (error.code === "mfa_required") {
const { primaryMethod, phoneNumber, secondaryMethod, token } =
@@ -961,11 +1025,20 @@ async function login(
navigate("mfa:code", {
...form,
- token,
selectedMethod: primaryMethod,
primaryMethod,
phoneNumber,
- secondaryMethod
+ secondaryMethod,
+ token
+ });
+ } else if (error.code === "password_required") {
+ const { token } = error.data as PasswordRequiredErrorData;
+
+ navigate("login:password", {
+ ...form,
+ token,
+ // TODO
+ password: ""
});
} else throw e;
}