|
|
|
|
@ -14,7 +14,7 @@ import isString from 'lodash/isString' |
|
|
|
|
import isBoolean from 'lodash/isBoolean' |
|
|
|
|
import includes from 'lodash/includes' |
|
|
|
|
|
|
|
|
|
import { PAGES } from 'config' |
|
|
|
|
import { PAGES, isIOS } from 'config' |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
addLanguageUrlParam, |
|
|
|
|
@ -24,6 +24,7 @@ import { |
|
|
|
|
setCookie, |
|
|
|
|
removeCookie, |
|
|
|
|
isMatchPage, |
|
|
|
|
getDomain, |
|
|
|
|
} from 'helpers' |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
@ -65,6 +66,14 @@ 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]) |
|
|
|
|
@ -79,6 +88,7 @@ export const useAuth = () => { |
|
|
|
|
removeToken() |
|
|
|
|
if (key !== 'saveToken') { |
|
|
|
|
removeCookie('access_token') |
|
|
|
|
removeCookie('refresh_token') |
|
|
|
|
} |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [lang]) |
|
|
|
|
@ -158,12 +168,21 @@ export const useAuth = () => { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const signinRedirectCallback = useCallback(() => { |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
userManager.signinRedirectCallback() |
|
|
|
|
.then((loadedUser) => { |
|
|
|
|
storeUser(loadedUser) |
|
|
|
|
|
|
|
|
|
if (isIOS && refreshToken) saveRefreshToken(refreshToken) |
|
|
|
|
|
|
|
|
|
queryParamStorage.clear() |
|
|
|
|
if (page.includes(PAGES.useraccount)) { |
|
|
|
|
history.push(PAGES.home) |
|
|
|
|
@ -187,10 +206,13 @@ 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 isRedirectedBackFromAuthProvider = Boolean(searchToken && searchRefToken && searchExp) |
|
|
|
|
|
|
|
|
|
isRedirectedBackFromAuthProvider ? signinRedirectCallback() : checkUser() |
|
|
|
|
isRedirectedBackFromAuthProvider |
|
|
|
|
? signinRedirectCallback(refreshToken) |
|
|
|
|
: checkUser() |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [ |
|
|
|
|
checkUser, |
|
|
|
|
|