Skip to content

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

web: redirect if logged in user navigates to unauthorized routes (fixes #907)

Abdullah Attabd3b5a4b98ce
diff --git a/apps/web/__e2e__/models/auth.model.ts b/apps/web/__e2e__/models/auth.model.ts
index e0420314e..c06edf991 100644
--- a/apps/web/__e2e__/models/auth.model.ts
+++ b/apps/web/__e2e__/models/auth.model.ts
@@ -41,6 +41,12 @@ export class AuthModel {
async goto() {
await this.page.goto("/login");
+ await this.ready();
+ }
+
+ async ready() {
+ await this.submitButton.waitFor({ state: "visible" });
+ return true;
}
async login(user: Partial<User>) {
diff --git a/apps/web/__e2e__/user.test.ts b/apps/web/__e2e__/user.test.ts
index bee45b6ab..0ef92e1e7 100644
--- a/apps/web/__e2e__/user.test.ts
+++ b/apps/web/__e2e__/user.test.ts
@@ -78,6 +78,31 @@ test("login user & wait for sync", async ({ page }, info) => {
expect(await app.isSynced()).toBeTruthy();
});
+test("logged in user should not be able to open unauthorized routes", async ({
+ page
+}, info) => {
+ info.setTimeout(45 * 1000);
+
+ const app = new AppModel(page);
+ await app.auth.goto();
+ await app.auth.login(USER.CURRENT);
+
+ const unauthorizedRoutes = [
+ "/login",
+ "/signup",
+ "/recover",
+ "/mfa/code",
+ "/mfa/select"
+ ] as const;
+
+ for (const route of unauthorizedRoutes) {
+ await page.goto(route);
+
+ await page.waitForURL(/\/notes/gm);
+ expect(await app.navigation.findItem("Notes")).toBeDefined();
+ }
+});
+
// test("login user and change password repeatedly", async ({
// page,
// browserName
diff --git a/apps/web/src/views/auth.tsx b/apps/web/src/views/auth.tsx
index 314915ed1..2d2ecaa8e 100644
--- a/apps/web/src/views/auth.tsx
+++ b/apps/web/src/views/auth.tsx
@@ -144,8 +144,17 @@ const routePaths: Record<AuthRoutes, string> = {
"mfa:select": "/mfa/select"
};
+const authorizedRoutes: AuthRoutes[] = [
+ "login",
+ "signup",
+ "mfa:code",
+ "mfa:select",
+ "recover"
+];
+
function Auth(props: AuthProps) {
const [route, setRoute] = useState(props.route);
+ const [isReady, setIsReady] = useState(false);
const [storedFormData, setStoredFormData] = useState<
BaseFormData | undefined
>();
@@ -154,6 +163,18 @@ function Auth(props: AuthProps) {
window.history.replaceState({}, "", makeURL(routePaths[route]));
}, [route]);
+ const [isAppLoaded] = useDatabase();
+
+ useEffect(() => {
+ if (!isAppLoaded) return;
+ db.user?.getUser().then((user) => {
+ if (user && authorizedRoutes.includes(route)) return openURL("/");
+ setIsReady(true);
+ });
+ }, [isAppLoaded, route]);
+
+ if (!isReady) return <></>;
+
return (
<AuthContainer>
<Flex
@@ -222,7 +243,6 @@ export default Auth;
function Login(props: BaseAuthComponentProps<"login">) {
const { navigate } = props;
- const [isAppLoaded] = useDatabase();
return (
<AuthForm
@@ -267,11 +287,7 @@ function Login(props: BaseAuthComponentProps<"login">) {
>
Forgot password?
</Button>
- <SubmitButton
- text="Login to your account"
- disabled={!isAppLoaded}
- loading={!isAppLoaded}
- />
+ <SubmitButton text="Login to your account" />
</>
)}
</AuthForm>
@@ -280,7 +296,6 @@ function Login(props: BaseAuthComponentProps<"login">) {
function Signup(props: BaseAuthComponentProps<"signup">) {
const { navigate } = props;
- const [isAppLoaded] = useDatabase();
return (
<AuthForm
@@ -329,11 +344,7 @@ function Signup(props: BaseAuthComponentProps<"signup">) {
label="Confirm password"
defaultValue={form?.["confirm-password"]}
/>
- <SubmitButton
- text="Create account"
- disabled={!isAppLoaded}
- loading={!isAppLoaded}
- />
+ <SubmitButton text="Create account" />
<Text
mt={4}
variant="subBody"
@@ -366,11 +377,9 @@ function Signup(props: BaseAuthComponentProps<"signup">) {
function SessionExpiry(props: BaseAuthComponentProps<"sessionExpiry">) {
const { navigate } = props;
- const [isAppLoaded] = useDatabase();
const [user, setUser] = useState<User | undefined>();
useEffect(() => {
- if (!isAppLoaded) return;
(async () => {
const user = await db.user?.getUser();
const isSessionExpired = Config.get("sessionExpired", false);
@@ -381,7 +390,7 @@ function SessionExpiry(props: BaseAuthComponentProps<"sessionExpiry">) {
openURL("/login");
} else openURL("/");
})();
- }, [isAppLoaded]);
+ }, []);
return (
<AuthForm
@@ -432,18 +441,13 @@ function SessionExpiry(props: BaseAuthComponentProps<"sessionExpiry">) {
>
Forgot password?
</Button>
- <SubmitButton
- text="Relogin to your account"
- disabled={!isAppLoaded}
- loading={!isAppLoaded}
- />
+ <SubmitButton text="Relogin to your account" />
</AuthForm>
);
}
function AccountRecovery(props: BaseAuthComponentProps<"recover">) {
const { navigate, formData } = props;
- const [isAppLoaded] = useDatabase();
const [success, setSuccess] = useState<string>();
return (
@@ -485,11 +489,7 @@ function AccountRecovery(props: BaseAuthComponentProps<"recover">) {
{success}
</Text>
</Flex>
- <SubmitButton
- text="Send again"
- disabled={!isAppLoaded}
- loading={!isAppLoaded}
- />
+ <SubmitButton text="Send again" />
</>
) : (
<>
@@ -502,11 +502,7 @@ function AccountRecovery(props: BaseAuthComponentProps<"recover">) {
defaultValue={formData ? formData.email : ""}
autoFocus
/>
- <SubmitButton
- text="Send recovery email"
- disabled={!isAppLoaded}
- loading={!isAppLoaded}
- />
+ <SubmitButton text="Send recovery email" />
</>
)}
</AuthForm>
@@ -548,7 +544,6 @@ function getTexts(formData: MFAFormData) {
function MFACode(props: BaseAuthComponentProps<"mfa:code">) {
const { navigate, formData } = props;
- const [isAppLoaded] = useDatabase();
const [isSending, setIsSending] = useState(false);
const { elapsed, enabled, setEnabled } = useTimer(
`2fa.${formData?.primaryMethod}`,
@@ -646,11 +641,7 @@ function MFACode(props: BaseAuthComponentProps<"mfa:code">) {
: undefined
}
/>
- <SubmitButton
- text="Submit"
- disabled={!isAppLoaded}
- loading={!isAppLoaded}
- />
+ <SubmitButton text="Submit" />
<Button
type="button"
mt={4}