1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

fix: reset loader when fetch receives 401 and fix no auth type (#549)

This commit is contained in:
Fredrik Strand Oseberg 2021-12-06 15:43:08 +01:00 committed by GitHub
parent f8710e61cc
commit 1f133beb46
4 changed files with 28 additions and 6 deletions

View File

@ -28,13 +28,24 @@ interface IAppProps extends RouteComponentProps {
const App = ({ location, user, fetchUiBootstrap }: IAppProps) => { const App = ({ location, user, fetchUiBootstrap }: IAppProps) => {
const { toast, setToastData } = useToast(); const { toast, setToastData } = useToast();
// because we need the userId when the component load. // because we need the userId when the component load.
const { splash, user: userFromUseUser } = useUser(); const { splash, user: userFromUseUser, authDetails } = useUser();
const [showSplash, setShowSplash] = useState(false); const [showSplash, setShowSplash] = useState(false);
const [showLoader, setShowLoader] = useState(false);
useEffect(() => { useEffect(() => {
fetchUiBootstrap(); fetchUiBootstrap();
/* eslint-disable-next-line */ /* eslint-disable-next-line */
}, [user.authDetails?.type]); }, [user.authDetails?.type]);
useEffect(() => {
// Temporary duality until redux store is removed
if (!isUnauthorized() && !userFromUseUser?.id && !authDetails) {
setShowLoader(true);
return;
}
setShowLoader(false);
/* eslint-disable-next-line */
}, [user.authDetails, userFromUseUser.id]);
useEffect(() => { useEffect(() => {
if (splash?.environment === undefined) return; if (splash?.environment === undefined) return;
if (!splash?.environment && !isUnauthorized()) { if (!splash?.environment && !isUnauthorized()) {
@ -92,9 +103,10 @@ const App = ({ location, user, fetchUiBootstrap }: IAppProps) => {
<SWRProvider <SWRProvider
setToastData={setToastData} setToastData={setToastData}
isUnauthorized={isUnauthorized} isUnauthorized={isUnauthorized}
setShowLoader={setShowLoader}
> >
<ConditionallyRender <ConditionallyRender
condition={!isUnauthorized() && !userFromUseUser?.id} condition={showLoader}
show={<Loader />} show={<Loader />}
elseShow={ elseShow={
<div className={styles.container}> <div className={styles.container}>

View File

@ -5,6 +5,7 @@ import { IToast } from '../../../hooks/useToast';
interface ISWRProviderProps { interface ISWRProviderProps {
setToastData: (toastData: IToast) => void; setToastData: (toastData: IToast) => void;
setShowLoader: React.Dispatch<React.SetStateAction<boolean>>;
isUnauthorized: () => boolean; isUnauthorized: () => boolean;
} }
@ -12,15 +13,16 @@ const SWRProvider: React.FC<ISWRProviderProps> = ({
children, children,
setToastData, setToastData,
isUnauthorized, isUnauthorized,
setShowLoader,
}) => { }) => {
const { cache } = useSWRConfig(); const { cache } = useSWRConfig();
const history = useHistory(); const history = useHistory();
const handleFetchError = error => { const handleFetchError = error => {
setShowLoader(false);
if (error.status === 401) { if (error.status === 401) {
cache.clear(); cache.clear();
const path = location.pathname; const path = location.pathname;
mutate(USER_CACHE_KEY, { ...error.info }, false); mutate(USER_CACHE_KEY, { ...error.info }, false);
if (path === '/login') { if (path === '/login') {
return; return;

View File

@ -25,7 +25,6 @@ const Login = () => {
}, [permissions.length]); }, [permissions.length]);
const resetPassword = query.get('reset') === 'true'; const resetPassword = query.get('reset') === 'true';
return ( return (
<StandaloneLayout> <StandaloneLayout>
<div className={styles.loginFormContainer}> <div className={styles.loginFormContainer}>

View File

@ -5,6 +5,7 @@ import { IPermission } from '../../../../interfaces/user';
import handleErrorResponses from '../httpErrorResponseHandler'; import handleErrorResponses from '../httpErrorResponseHandler';
export const USER_CACHE_KEY = `api/admin/user`; export const USER_CACHE_KEY = `api/admin/user`;
const NO_AUTH_USERNAME = 'unknown';
const useUser = ( const useUser = (
options: SWRConfiguration = { options: SWRConfiguration = {
@ -33,12 +34,20 @@ const useUser = (
setLoading(!error && !data); setLoading(!error && !data);
}, [data, error]); }, [data, error]);
let user = data?.user;
// Set a user id if no authentication is on
// to cancel the loader.
if (data && user?.username === NO_AUTH_USERNAME) {
user = { ...user, id: 1 };
}
return { return {
user: data?.user || {}, user: user || {},
permissions: (data?.permissions || []) as IPermission[], permissions: (data?.permissions || []) as IPermission[],
feedback: data?.feedback || [], feedback: data?.feedback || [],
splash: data?.splash || {}, splash: data?.splash || {},
authDetails: data || {}, authDetails: data || undefined,
error, error,
loading, loading,
refetch, refetch,