-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
214 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './useAppRedirectOnGotData'; | ||
export * from './useStartInitialLoad'; | ||
export * from './useAuthCheck'; | ||
export * from './useDisplayAuthError'; | ||
export * from './useLogoutRedirect'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useEffect } from 'react'; | ||
import { useHistory, useLocation } from 'react-router-dom'; | ||
|
||
export const useAppRedirectOnGotData = ( | ||
hasData: boolean, | ||
setShouldFetchUserData: (shouldFetchUserData: boolean) => void | ||
) => { | ||
const history = useHistory(); | ||
const location = useLocation(); | ||
|
||
useEffect(() => { | ||
if (hasData) { | ||
if (!location.pathname.includes('app')) { | ||
history.push('/app'); | ||
} | ||
setShouldFetchUserData(false); | ||
} | ||
}, [hasData, history, location.pathname, setShouldFetchUserData]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import to from 'await-to-js'; | ||
import { useState, useCallback } from 'react'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { useAuth } from '../../components/general/AuthWrapper/AuthWrapper'; | ||
import { i18nInitPromise } from '../../i18n'; | ||
import { NonAppLocation } from '../../main/App'; | ||
import { authenticate } from '../../net/requests'; | ||
import { useDisplayAuthError } from './useDisplayAuthError'; | ||
|
||
export interface AuthError { | ||
initialLocation: string | null; | ||
err: boolean; | ||
} | ||
|
||
export const useAuthCheck = ( | ||
setShouldFetchUserData: (shouldFetchUserData: boolean) => void | ||
) => { | ||
const auth = useAuth(); | ||
const history = useHistory(); | ||
|
||
const [authErr, setAuthErr] = useState<AuthError>({ | ||
initialLocation: null, | ||
err: false, | ||
}); | ||
|
||
const onAuth = useCallback(async () => { | ||
if (history.location.pathname === NonAppLocation.Account) { | ||
return; | ||
} | ||
|
||
const [err] = await to(authenticate({ token: auth.token })); | ||
if (err !== null) { | ||
await i18nInitPromise; | ||
|
||
setAuthErr({ | ||
initialLocation: history.location.pathname, | ||
err: true, | ||
}); | ||
history.push(NonAppLocation.Account); | ||
} else { | ||
setShouldFetchUserData(true); | ||
} | ||
}, [auth.token, history, setShouldFetchUserData]); | ||
|
||
useDisplayAuthError(authErr, setAuthErr); | ||
|
||
return { onAuth }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useToast } from '@chakra-ui/react'; | ||
import { useEffect } from 'react'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { i18nInitPromise } from '../../i18n'; | ||
import { AuthError } from './useAuthCheck'; | ||
|
||
export const useDisplayAuthError = ( | ||
authErr: AuthError, | ||
setAuthErr: React.Dispatch<React.SetStateAction<AuthError>> | ||
) => { | ||
const showToast = useToast(); | ||
const history = useHistory(); | ||
|
||
useEffect(() => { | ||
if (authErr.err && authErr.initialLocation !== '/') { | ||
i18nInitPromise.then((t) => { | ||
showToast({ | ||
title: t('prompt-login-title'), | ||
description: t('prompt-login-info'), | ||
status: 'error', | ||
duration: 5000, | ||
isClosable: true, | ||
}); | ||
}); | ||
setAuthErr((prev) => { | ||
return { | ||
...prev, | ||
err: false, | ||
}; | ||
}); | ||
} | ||
}, [authErr, history.location.pathname, setAuthErr, showToast]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect } from 'react'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { NonAppLocation } from '../../main/App'; | ||
|
||
export const useLogoutRedirect = ( | ||
isLoggingOut: boolean, | ||
hasData: boolean, | ||
shouldFetchUserData: boolean | ||
) => { | ||
const history = useHistory(); | ||
|
||
useEffect(() => { | ||
if (isLoggingOut && !hasData && !shouldFetchUserData) { | ||
history.push(NonAppLocation.Account); | ||
} | ||
}, [hasData, history, isLoggingOut, shouldFetchUserData]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect } from 'react'; | ||
import { InitialLoadData } from '../../main/App'; | ||
|
||
export const useStartInitialLoad = ( | ||
initialLoad: () => Promise<void>, | ||
setInitialLoadData: React.Dispatch<React.SetStateAction<InitialLoadData>> | ||
) => { | ||
useEffect(() => { | ||
setInitialLoadData((prevData) => { | ||
return { | ||
...prevData, | ||
promiseList: [...prevData.promiseList, initialLoad()], | ||
startInitialLoad: true, | ||
}; | ||
}); | ||
}, [initialLoad, setInitialLoadData]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters