From 2ee41be79c144724e874ddb347db46c2c7a72114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aron=20Sch=C3=BCler?= Date: Sat, 9 Mar 2024 19:13:14 +0100 Subject: [PATCH] feat: correct auth redirect target --- src/router.tsx | 2 +- .../{RedirectIfNotLoggedIn.tsx => AuthRedirects.tsx} | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) rename src/utils/{RedirectIfNotLoggedIn.tsx => AuthRedirects.tsx} (55%) diff --git a/src/router.tsx b/src/router.tsx index 83dbde5..ebfcde2 100644 --- a/src/router.tsx +++ b/src/router.tsx @@ -5,7 +5,7 @@ import { Authentication } from "./views/Authentication"; import { RedirectIfNotLoggedIn, RedirectIfSignedIn, -} from "./utils/RedirectIfNotLoggedIn"; +} from "./utils/AuthRedirects"; import { Goals } from "./views/Goals"; import { NewLayout } from "./views/NewLayout"; import { routes } from "./routes"; diff --git a/src/utils/RedirectIfNotLoggedIn.tsx b/src/utils/AuthRedirects.tsx similarity index 55% rename from src/utils/RedirectIfNotLoggedIn.tsx rename to src/utils/AuthRedirects.tsx index 7b46a55..8efb298 100644 --- a/src/utils/RedirectIfNotLoggedIn.tsx +++ b/src/utils/AuthRedirects.tsx @@ -5,11 +5,17 @@ import { UserContext } from "../provider/UserContext"; export const RedirectIfNotLoggedIn = ({ children }: PropsWithChildren) => { const { user } = useContext(UserContext); - return !user?.uid ? : children; + const target = "/login" + `?redirect=${window.location.pathname}`; + + return !user?.uid ? : children; }; export const RedirectIfSignedIn = ({ children }: PropsWithChildren) => { const { user } = useContext(UserContext); - return user?.uid ? : children; + const query = new URLSearchParams(window.location.search); + + const target = query.get("redirect") || "/dashboard"; + + return user?.uid ? : children; };