From b7a80e383aa2f5a5b4e4dbe914115a6ad5ca453b Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 19 Jan 2023 12:02:38 +0100 Subject: [PATCH] improved get next url handling --- src/hooks/use-navigate-to-next.ts | 18 +++--------------- src/routes/EnterDecryptionPassword.tsx | 20 ++++++++++++++++---- src/utils/get-next-url.ts | 12 ++++++++++++ src/utils/index.ts | 2 ++ 4 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 src/utils/get-next-url.ts diff --git a/src/hooks/use-navigate-to-next.ts b/src/hooks/use-navigate-to-next.ts index 1cff7b8..5dc3cdf 100644 --- a/src/hooks/use-navigate-to-next.ts +++ b/src/hooks/use-navigate-to-next.ts @@ -1,26 +1,14 @@ import {useLocation, useNavigate} from "react-router-dom" import {useCallback} from "react" +import {getNextUrl} from "~/utils" + export default function useNavigateToNext(defaultNextUrl = "/"): () => void { const navigate = useNavigate() const location = useLocation() const navigateToNext = useCallback(() => { - const nextUrlSuggested = - new URLSearchParams(location.search).get("next") || "" - - const nextUrl = (() => { - if ( - nextUrlSuggested.startsWith("/") || - nextUrlSuggested.startsWith(`https://${window.location.host}`) - ) { - return nextUrlSuggested - } - - return defaultNextUrl - })() - - setTimeout(() => navigate(nextUrl), 0) + setTimeout(() => navigate(getNextUrl(defaultNextUrl)), 0) }, [location, navigate]) return navigateToNext diff --git a/src/routes/EnterDecryptionPassword.tsx b/src/routes/EnterDecryptionPassword.tsx index 94fe217..5ff8445 100644 --- a/src/routes/EnterDecryptionPassword.tsx +++ b/src/routes/EnterDecryptionPassword.tsx @@ -1,15 +1,15 @@ import * as yup from "yup" -import {ReactElement, useContext} from "react" +import {ReactElement, useContext, useLayoutEffect} from "react" import {useFormik} from "formik" import {MdLock} from "react-icons/md" import {useTranslation} from "react-i18next" -import {useLoaderData} from "react-router-dom" +import {useLoaderData, useNavigate} from "react-router-dom" import {InputAdornment} from "@mui/material" import {useNavigateToNext, useUser} from "~/hooks" -import {AuthContext, PasswordField, SimpleForm} from "~/components" -import {getMasterPassword} from "~/utils" +import {AuthContext, EncryptionStatus, PasswordField, SimpleForm} from "~/components" +import {getMasterPassword, getNextUrl} from "~/utils" import {ServerSettings} from "~/server-types" interface Form { @@ -18,8 +18,10 @@ interface Form { export default function EnterDecryptionPassword(): ReactElement { const {t} = useTranslation() + const navigate = useNavigate() const navigateToNext = useNavigateToNext() const user = useUser() + const {encryptionStatus} = useContext(AuthContext) const serverSettings = useLoaderData() as ServerSettings const {_setEncryptionPassword} = useContext(AuthContext) @@ -52,6 +54,16 @@ export default function EnterDecryptionPassword(): ReactElement { }, }) + useLayoutEffect(() => { + if (encryptionStatus === EncryptionStatus.Unavailable) { + const nextUrl = getNextUrl() + + navigate(`/auth/complete-account?setup=true&next=${nextUrl}`, { + replace: true, + }) + } + }, [encryptionStatus]) + return (