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 { 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,
])

Loading…
Cancel
Save