feat(ott-1579): added redirect to match after login (#471) (#473)

* feat(ott-1579): added redirect to match after login

* feat(ott-1579): add uselocalstore hook

Co-authored-by: PolyakovaM <55061222+PolyakovaM@users.noreply.github.com>
keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
Mirlan 4 years ago committed by GitHub
parent bb756f4109
commit d7416486a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      src/features/AuthStore/hooks/useAuth.tsx

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

Loading…
Cancel
Save