diff --git a/src/features/AuthStore/hooks/useAuth.tsx b/src/features/AuthStore/hooks/useAuth.tsx index 06298acf..3ba473fb 100644 --- a/src/features/AuthStore/hooks/useAuth.tsx +++ b/src/features/AuthStore/hooks/useAuth.tsx @@ -9,12 +9,14 @@ import { useHistory } from 'react-router' import type { User } from 'oidc-client' import { UserManager } from 'oidc-client' +import isString from 'lodash/isString' + import { PAGES } from 'config' import { writeToken, removeToken } from 'helpers/token' import { setCookie, removeCookie } from 'helpers/cookie' -import { useToggle } from 'hooks' +import { useLocalStore, useToggle } from 'hooks' import { queryParamStorage } from 'features/QueryParamsStorage' import { isMatchPage } from 'features/JoinMatchPage/helpers' @@ -66,17 +68,30 @@ export const useAuth = () => { markUserLoaded, ]) + const [matchPage, setMatchPage] = useLocalStore({ + clearOnUnmount: true, + defaultValue: '', + key: 'matchBackLocation', + validator: isString, + }) + const signinRedirectCallback = useCallback(async () => { const loadedUser = await userManager.signinRedirectCallback() storeUser(loadedUser) queryParamStorage.clear() history.replace(PAGES.home) markUserLoaded() + if (matchPage) { + history.push(matchPage) + setMatchPage('') + } }, [ userManager, history, + setMatchPage, storeUser, markUserLoaded, + matchPage, ]) useEffect(() => { @@ -85,13 +100,19 @@ export const useAuth = () => { signinRedirectCallback() } else { checkUser().catch(() => { - if (!isMatchPage()) login() + if (!isMatchPage()) { + login() + setMatchPage('') + } else { + setMatchPage(history.location.pathname) + } }) } }, [ checkUser, signinRedirectCallback, login, + setMatchPage, history, ])