Handle isLoading state in private routes

Additional check for isLoading to handle
redirect after sign-up.
This commit is contained in:
Tom Russell 2022-07-22 16:05:20 +01:00
parent e478e182a5
commit 187dc20baa

View File

@ -1,10 +1,11 @@
import React from 'react'
import React, { Fragment } from 'react'
import { Route, RouteProps, Redirect } from 'react-router-dom';
import { SpinnerIcon } from './components/icons';
import { useAuth } from './auth-context';
export const PrivateRoute: React.FC<RouteProps> = ({component: Component, children, ...rest}) => {
const { isAuthenticated } = useAuth();
const { isAuthenticated, isLoading } = useAuth();
return <Route
{...rest}
@ -15,14 +16,17 @@ export const PrivateRoute: React.FC<RouteProps> = ({component: Component, childr
} else if(children) {
return <>{children}</>;
}
} else {
if (isLoading) {
return <Fragment><SpinnerIcon spin={true} /> Loading user info...</Fragment>
}
return <Redirect to={{
pathname: "/login.html",
state: { from: props.location.pathname }
}} />;
}
}}
}}
/>
};
@ -47,6 +51,6 @@ export const AuthRoute: React.FC<RouteProps> = ({ component: Component, children
return <>{children}</>;
}
}
}}
}}
/>
};