feat(ott-2113): change redirect after auth

keep-around/efafe8ea63ccf54ee38a4c03f17b8468f4023163
nevainero 4 years ago
parent 6537ca4b49
commit efafe8ea63
  1. 48
      src/features/AuthStore/hooks/useAuth.tsx

@ -13,7 +13,9 @@ import isString from 'lodash/isString'
import { PAGES } from 'config'
import { addLanguageUrlParam } from 'helpers/languageUrlParam'
import {
addLanguageUrlParam,
} from 'helpers/languageUrlParam'
import { writeToken, removeToken } from 'helpers/token'
import { setCookie, removeCookie } from 'helpers/cookie'
import { isMatchPage } from 'helpers/isMatchPage'
@ -21,7 +23,9 @@ import { isMatchPage } from 'helpers/isMatchPage'
import { useLocalStore, useToggle } from 'hooks'
import { useLexicsStore } from 'features/LexicsStore'
import { queryParamStorage } from 'features/QueryParamsStorage'
import {
queryParamStorage,
} from 'features/QueryParamsStorage'
import { getClientSettings } from '../helpers'
@ -72,13 +76,20 @@ export const useAuth = () => {
markUserLoaded,
])
const [matchPage, setMatchPage] = useLocalStore({
const [page, setPage] = useLocalStore({
clearOnUnmount: true,
defaultValue: '',
key: 'matchBackLocation',
validator: isString,
})
const [search, setSearch] = useLocalStore({
clearOnUnmount: true,
defaultValue: '',
key: 'searchBack',
validator: isString,
})
const signinRedirectCallback = useCallback(() => {
userManager.signinRedirectCallback()
.then((loadedUser) => {
@ -86,19 +97,23 @@ export const useAuth = () => {
queryParamStorage.clear()
history.replace(PAGES.home)
markUserLoaded()
if (matchPage) {
history.push(matchPage)
setMatchPage('')
if (page) {
const route = `${page}${page === '/' ? search : ''}`
history.push(route)
setPage('')
setSearch('')
}
}).catch(login)
}, [
userManager,
history,
setMatchPage,
login,
storeUser,
history,
markUserLoaded,
matchPage,
login,
page,
search,
setPage,
setSearch,
])
useEffect(() => {
@ -109,17 +124,19 @@ export const useAuth = () => {
checkUser().catch(() => {
if (!isMatchPage()) {
login()
setMatchPage('')
} else {
setMatchPage(history.location.pathname)
}
if (history.location.pathname === '/') {
setSearch(history.location.search)
}
setPage(history.location.pathname)
})
}
}, [
checkUser,
signinRedirectCallback,
login,
setMatchPage,
setPage,
setSearch,
history,
])
@ -140,7 +157,8 @@ export const useAuth = () => {
}, [userManager, logout])
useEffect(() => {
// событие срабатывает после получения токена(первый логин и обновление токена)
// событие срабатывает после получения токена(первый
// логин и обновление токена)
userManager.events.addUserLoaded(storeUser)
return () => userManager.events.removeUserLoaded(storeUser)
}, [userManager, storeUser])

Loading…
Cancel
Save