From 8b7aae1f0df59977a7236a69fbe08f6e04ce124e Mon Sep 17 00:00:00 2001 From: Aron Buzogany <108480125+AronBuzogany@users.noreply.github.com> Date: Thu, 23 May 2024 20:46:05 +0200 Subject: [PATCH] redirecting to homepage when user not logged in (#421) * redirecting to homepage when user not logged in * linting --- frontend/src/App.tsx | 1 + frontend/src/components/Header/Layout.tsx | 41 ++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 6d2cad70..a3c39927 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -35,6 +35,7 @@ const router = createBrowserRouter( > } loader={fetchProjectPage} /> }> + } loader={fetchProjectPage} /> } loader={fetchProjectPage} /> { + if ( + !meData.loggedIn && + !( + location.pathname === "/" || + /\/([a-z]{2})?\/home/.test(location.pathname) + ) + ) { + navigate("/"); + } + }, [meData.loggedIn, location.pathname, navigate]); return ( <> @@ -16,3 +38,20 @@ export default function Layout(): JSX.Element { ); } + +const useEnsureLangCodeInPath = () => { + const location = useLocation(); + const navigate = useNavigate(); + + useEffect(() => { + const pathParts = location.pathname.split("/").filter(Boolean); + const langCode = i18next.resolvedLanguage; + + // Check if the URL starts with the lang code + if (pathParts[0] !== langCode) { + // Prepend the lang code to the path + const newPath = `/${langCode}/${pathParts.join("/")}`; + navigate(newPath); + } + }, [location, navigate]); +}; \ No newline at end of file