|
|
|
|
@ -14,7 +14,11 @@ import isString from 'lodash/isString' |
|
|
|
|
import isBoolean from 'lodash/isBoolean' |
|
|
|
|
import includes from 'lodash/includes' |
|
|
|
|
|
|
|
|
|
import { PAGES, isIOS } from 'config' |
|
|
|
|
import { |
|
|
|
|
PAGES, |
|
|
|
|
isFacrClient, |
|
|
|
|
isLffClient, |
|
|
|
|
} from 'config' |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
addLanguageUrlParam, |
|
|
|
|
@ -24,7 +28,10 @@ import { |
|
|
|
|
setCookie, |
|
|
|
|
removeCookie, |
|
|
|
|
isMatchPage, |
|
|
|
|
getDomain, |
|
|
|
|
REFRESH_TOKEN_KEY, |
|
|
|
|
removeRefreshToken, |
|
|
|
|
writeRefreshToken, |
|
|
|
|
readRefreshToken, |
|
|
|
|
} from 'helpers' |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
@ -66,14 +73,6 @@ export const useAuth = () => { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const parseJwt = (value: string) => { |
|
|
|
|
const base64Url = value.split('.')[1] |
|
|
|
|
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/') |
|
|
|
|
const jsonPayload = decodeURIComponent(window.atob(base64).split('').map((c) => `%${(`00${c.charCodeAt(0).toString(16)}`).slice(-2)}`).join('')) |
|
|
|
|
|
|
|
|
|
return JSON.parse(jsonPayload) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const login = useCallback(async () => { |
|
|
|
|
userManager.signinRedirect({ extraQueryParams: { lang } }) |
|
|
|
|
}, [lang]) |
|
|
|
|
@ -86,9 +85,9 @@ export const useAuth = () => { |
|
|
|
|
userManager.signoutRedirect({ post_logout_redirect_uri: urlWithLang }) |
|
|
|
|
}) |
|
|
|
|
removeToken() |
|
|
|
|
removeRefreshToken() |
|
|
|
|
if (key !== 'saveToken') { |
|
|
|
|
removeCookie('access_token') |
|
|
|
|
removeCookie('refresh_token') |
|
|
|
|
} |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [lang]) |
|
|
|
|
@ -168,12 +167,6 @@ export const useAuth = () => { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const saveRefreshToken = (value: string) => { |
|
|
|
|
const ref = parseJwt(value) |
|
|
|
|
const expires = `expires=${new Date((ref.exp * 1000)).toUTCString()}` |
|
|
|
|
document.cookie = `refresh_token=${value};${expires};path=/;domain=${getDomain()};secure;SameSite=None` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const signinRedirectCallback = useCallback(async (refreshToken: string | null) => { |
|
|
|
|
setPage(history.location.pathname) |
|
|
|
|
|
|
|
|
|
@ -181,7 +174,10 @@ export const useAuth = () => { |
|
|
|
|
.then((loadedUser) => { |
|
|
|
|
storeUser(loadedUser) |
|
|
|
|
|
|
|
|
|
if (isIOS && refreshToken) saveRefreshToken(refreshToken) |
|
|
|
|
if ( |
|
|
|
|
refreshToken |
|
|
|
|
&& (isLffClient || isFacrClient) |
|
|
|
|
) writeRefreshToken(refreshToken) |
|
|
|
|
|
|
|
|
|
queryParamStorage.clear() |
|
|
|
|
if (page.includes(PAGES.useraccount)) { |
|
|
|
|
@ -194,7 +190,7 @@ export const useAuth = () => { |
|
|
|
|
setPage('') |
|
|
|
|
setSearch('') |
|
|
|
|
}).catch(login) |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [ |
|
|
|
|
login, |
|
|
|
|
storeUser, |
|
|
|
|
@ -206,14 +202,14 @@ export const useAuth = () => { |
|
|
|
|
const searchToken = urlSearch.get('access_token') |
|
|
|
|
const searchRefToken = urlSearch.get('id_token') |
|
|
|
|
const searchExp = urlSearch.get('expires_in') |
|
|
|
|
const refreshToken = urlSearch.get('refresh_token') |
|
|
|
|
const refreshToken = urlSearch.get(REFRESH_TOKEN_KEY) |
|
|
|
|
|
|
|
|
|
const isRedirectedBackFromAuthProvider = Boolean(searchToken && searchRefToken && searchExp) |
|
|
|
|
|
|
|
|
|
isRedirectedBackFromAuthProvider |
|
|
|
|
? signinRedirectCallback(refreshToken) |
|
|
|
|
: checkUser() |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [ |
|
|
|
|
checkUser, |
|
|
|
|
signinRedirectCallback, |
|
|
|
|
@ -243,7 +239,7 @@ export const useAuth = () => { |
|
|
|
|
}, [reChekNewDevice]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (!needCheckNewDeviсe && !user) return undefined |
|
|
|
|
if (!needCheckNewDeviсe || !user) return undefined |
|
|
|
|
const startCheckDevice = setInterval(checkNewDevice, 20000) |
|
|
|
|
isNewDeviceLogin && clearInterval(startCheckDevice) |
|
|
|
|
return () => clearInterval(startCheckDevice) |
|
|
|
|
@ -262,7 +258,13 @@ export const useAuth = () => { |
|
|
|
|
// библиотека oidc-client не поддерживает обновление токена только на 1 вкладке
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
if (window.isMaster()) { |
|
|
|
|
userManager.signinSilent().catch(logout) |
|
|
|
|
// safari ограничивает доступ к куке через крос доменные запросы
|
|
|
|
|
// передаем рефреш токен через квери параметры
|
|
|
|
|
userManager.signinSilent({ |
|
|
|
|
extraQueryParams: (isLffClient || isFacrClient) && { |
|
|
|
|
refresh_token: readRefreshToken(), |
|
|
|
|
}, |
|
|
|
|
}).catch(logout) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// если запросы вернули 401 | 403
|
|
|
|
|
|