From 7b6c1709049c0eb7abb47b7627229935beadfd9f Mon Sep 17 00:00:00 2001 From: LashaunnaS Date: Mon, 21 Oct 2024 14:02:59 -0400 Subject: [PATCH 1/4] Fix to the useSessionBanners hook to ensure that logic such as banner removal only occurs after authorization has changed. Replacing the useEffect with useMemo should ensure that any side effects that get triggered are due the state of the application once any further auth logic has been resolved. --- src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts b/src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts index 91cd43b2f..168deb90e 100644 --- a/src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts +++ b/src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts @@ -1,4 +1,4 @@ -import { useEffect, useRef } from 'react'; +import { useMemo, useRef } from 'react'; import { useBanners } from '~/entities/banner/model'; import { userModel } from '~/entities/user'; @@ -9,7 +9,7 @@ export const useSessionBanners = () => { const prevIsAuthorized = useRef(isAuthorized); - useEffect(() => { + useMemo(() => { if (prevIsAuthorized.current !== isAuthorized && !isAuthorized) { removeAllBanners(); } From f2d04c889a28d5b885f5ef183d5145367f502648 Mon Sep 17 00:00:00 2001 From: LashaunnaS Date: Tue, 22 Oct 2024 17:11:45 -0400 Subject: [PATCH 2/4] Store the original url so that when the app logs out the previously authenticated user, the newly authenticated user will get redirected to the route originally specified. --- src/entities/user/model/hooks/useOnLogin.ts | 2 +- src/features/Logout/lib/useLogout.ts | 17 +++++++++++++++-- src/pages/AuthorizedRoutes.tsx | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/entities/user/model/hooks/useOnLogin.ts b/src/entities/user/model/hooks/useOnLogin.ts index 2c115c958..52910e36d 100644 --- a/src/entities/user/model/hooks/useOnLogin.ts +++ b/src/entities/user/model/hooks/useOnLogin.ts @@ -41,7 +41,7 @@ export const useOnLogin = (params: Params) => { secureTokensStorage.setTokens(tokens); if (params.backRedirectPath !== undefined) { - navigate(params.backRedirectPath); + navigate(params.backRedirectPath, { replace: true }); } else { Mixpanel.track('Login Successful'); Mixpanel.login(user.id); diff --git a/src/features/Logout/lib/useLogout.ts b/src/features/Logout/lib/useLogout.ts index 8bd101f05..31f918e40 100644 --- a/src/features/Logout/lib/useLogout.ts +++ b/src/features/Logout/lib/useLogout.ts @@ -1,5 +1,7 @@ import { useCallback } from 'react'; +import { useLocation } from 'react-router-dom'; + import { appletModel } from '~/entities/applet'; import { useLogoutMutation, userModel } from '~/entities/user'; import { AutoCompletionModel } from '~/features/AutoCompletion'; @@ -14,6 +16,7 @@ type UseLogoutReturn = { export const useLogout = (): UseLogoutReturn => { const navigator = useCustomNavigation(); + const location = useLocation(); const { clearUser } = userModel.hooks.useUserState(); const { clearStore } = appletModel.hooks.useClearStore(); @@ -37,8 +40,18 @@ export const useLogout = (): UseLogoutReturn => { Mixpanel.track('logout'); Mixpanel.logout(); FeatureFlags.logout(); - return navigator.navigate(ROUTES.login.path); - }, [clearUser, clearStore, clearAutoCompletionState, navigator, logoutMutation]); + + const backRedirectPath = `${location.pathname}${location.search}`; + return navigator.navigate(ROUTES.login.path, { state: { backRedirectPath } }); + }, [ + clearUser, + clearStore, + clearAutoCompletionState, + location.pathname, + location.search, + navigator, + logoutMutation, + ]); return { logout, diff --git a/src/pages/AuthorizedRoutes.tsx b/src/pages/AuthorizedRoutes.tsx index a89b01516..18b802786 100644 --- a/src/pages/AuthorizedRoutes.tsx +++ b/src/pages/AuthorizedRoutes.tsx @@ -48,10 +48,10 @@ function AuthorizedRoutes({ refreshToken }: Props) { } /> } /> } /> - - } /> + + } /> ); From 8aa8fe0cc95d5ce8898ad9fee4244941bdef83e8 Mon Sep 17 00:00:00 2001 From: LashaunnaS Date: Tue, 22 Oct 2024 17:52:07 -0400 Subject: [PATCH 3/4] Capture possibility of of user coming from the loginroute once authenticated. --- src/pages/AuthorizedRoutes.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pages/AuthorizedRoutes.tsx b/src/pages/AuthorizedRoutes.tsx index 18b802786..5eeb0ee17 100644 --- a/src/pages/AuthorizedRoutes.tsx +++ b/src/pages/AuthorizedRoutes.tsx @@ -5,6 +5,7 @@ import { Navigate, Route, Routes } from 'react-router-dom'; import AppletDetailsPage from './AppletDetailsPage'; import AppletListPage from './AppletListPage'; import AutoCompletion from './AutoCompletion'; +import LoginPage from './Login'; import ProfilePage from './Profile'; import PublicAutoCompletion from './PublicAutoCompletion'; import SettingsPage from './Settings'; @@ -50,7 +51,7 @@ function AuthorizedRoutes({ refreshToken }: Props) { } /> - + } /> } /> From 63045fd2ca04da93b6c28af7c314fc1ca814fcf2 Mon Sep 17 00:00:00 2001 From: LashaunnaS Date: Wed, 23 Oct 2024 15:16:03 -0400 Subject: [PATCH 4/4] Remove use of useEffect hook. Which seems to be causing removeAllbanners to triggering after rerendering. --- .../hooks/useSessionBanners/useSessionBanners.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts b/src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts index 168deb90e..dd5d94fda 100644 --- a/src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts +++ b/src/shared/utils/hooks/useSessionBanners/useSessionBanners.ts @@ -1,4 +1,4 @@ -import { useMemo, useRef } from 'react'; +import { useRef } from 'react'; import { useBanners } from '~/entities/banner/model'; import { userModel } from '~/entities/user'; @@ -9,11 +9,9 @@ export const useSessionBanners = () => { const prevIsAuthorized = useRef(isAuthorized); - useMemo(() => { - if (prevIsAuthorized.current !== isAuthorized && !isAuthorized) { - removeAllBanners(); - } + if (prevIsAuthorized.current !== isAuthorized && !isAuthorized) { + removeAllBanners(); + } - prevIsAuthorized.current = isAuthorized; - }, [isAuthorized, removeAllBanners]); + prevIsAuthorized.current = isAuthorized; };