fix(ui): redirect to sign-in when user is not authorized (#8512)

This commit is contained in:
Chirag Madlani 2022-11-03 16:08:50 +05:30 committed by GitHub
parent f4cc2b57a7
commit 60a97ea8bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,7 @@ import SlackChat from '../components/SlackChat/SlackChat';
import { ROUTES } from '../constants/constants'; import { ROUTES } from '../constants/constants';
import { AuthTypes } from '../enums/signin.enum'; import { AuthTypes } from '../enums/signin.enum';
import AccountActivationConfirmation from '../pages/signup/account-activation-confirmation.component'; import AccountActivationConfirmation from '../pages/signup/account-activation-confirmation.component';
import { isProtectedRoute } from '../utils/AuthProvider.util';
import withSuspenseFallback from './withSuspenseFallback'; import withSuspenseFallback from './withSuspenseFallback';
const AuthenticatedAppRouter = withSuspenseFallback( const AuthenticatedAppRouter = withSuspenseFallback(
@ -55,7 +56,6 @@ const AppRouter = () => {
const { const {
authConfig, authConfig,
isAuthDisabled,
isAuthenticated, isAuthenticated,
loading, loading,
isSigningIn, isSigningIn,
@ -72,6 +72,11 @@ const AppRouter = () => {
const isOidcProvider = const isOidcProvider =
authConfig?.provider && oidcProviders.includes(authConfig.provider); authConfig?.provider && oidcProviders.includes(authConfig.provider);
const isBasicAuthProvider =
authConfig &&
(authConfig.provider === AuthTypes.BASIC ||
authConfig.provider === AuthTypes.LDAP);
const fetchSlackChatConfig = () => { const fetchSlackChatConfig = () => {
fetchSlackConfig() fetchSlackConfig()
.then((res) => { .then((res) => {
@ -114,83 +119,50 @@ const AppRouter = () => {
} }
}, [location.pathname]); }, [location.pathname]);
return loading ? ( if (loading) {
<Loader /> return <Loader />;
) : ( }
<>
{isOidcProvider || isAuthenticated ? ( if (!isAuthenticated && isProtectedRoute(location.pathname)) {
return <Redirect to={ROUTES.SIGNIN} />;
}
if (isOidcProvider || isAuthenticated) {
return (
<> <>
<AuthenticatedAppRouter /> <AuthenticatedAppRouter />
{slackChat} {slackChat}
</> </>
) : ( );
<> }
{slackChat}
<Switch>
<Route exact component={BasicSignupPage} path={ROUTES.REGISTER} />
return (
<Switch>
{slackChat}
<Route exact component={SigninPage} path={ROUTES.SIGNIN} />
{callbackComponent ? ( {callbackComponent ? (
<Route component={callbackComponent} path={ROUTES.CALLBACK} /> <Route component={callbackComponent} path={ROUTES.CALLBACK} />
) : null} ) : null}
<Route exact path={ROUTES.HOME}> <Route exact path={ROUTES.HOME}>
{!isAuthDisabled && !isAuthenticated && !isSigningIn ? ( {!isAuthenticated && !isSigningIn ? (
<> <>
<Redirect to={ROUTES.SIGNIN} /> <Redirect to={ROUTES.SIGNIN} />
<Route
exact
component={ForgotPassword}
path={ROUTES.FORGOT_PASSWORD}
/>
<Route
exact
component={ResetPassword}
path={ROUTES.RESET_PASSWORD}
/>
<Route
exact
component={AccountActivationConfirmation}
path={ROUTES.ACCOUNT_ACTIVATION}
/>
</> </>
) : ( ) : (
<Redirect to={ROUTES.MY_DATA} /> <Redirect to={ROUTES.MY_DATA} />
)} )}
</Route> </Route>
{!isSigningIn ? (
{isBasicAuthProvider && (
<> <>
<Route exact component={SigninPage} path={ROUTES.SIGNIN} /> <Route exact component={BasicSignupPage} path={ROUTES.REGISTER} />
<Route <Route
exact exact
component={ForgotPassword} component={ForgotPassword}
path={ROUTES.FORGOT_PASSWORD} path={ROUTES.FORGOT_PASSWORD}
/> />
<Route <Route exact component={ResetPassword} path={ROUTES.RESET_PASSWORD} />
exact
component={ResetPassword}
path={ROUTES.RESET_PASSWORD}
/>
<Route
exact
component={AccountActivationConfirmation}
path={ROUTES.ACCOUNT_ACTIVATION}
/>
</>
) : null}
{isAuthDisabled || isAuthenticated ? (
<AuthenticatedAppRouter />
) : (
<>
<Redirect to={ROUTES.SIGNIN} />
<Route
exact
component={ForgotPassword}
path={ROUTES.FORGOT_PASSWORD}
/>
<Route
exact
component={ResetPassword}
path={ROUTES.RESET_PASSWORD}
/>
<Route <Route
exact exact
component={AccountActivationConfirmation} component={AccountActivationConfirmation}
@ -198,11 +170,9 @@ const AppRouter = () => {
/> />
</> </>
)} )}
{isAuthenticated && <AuthenticatedAppRouter />}
<Route exact component={PageNotFound} path={ROUTES.NOT_FOUND} /> <Route exact component={PageNotFound} path={ROUTES.NOT_FOUND} />
</Switch> </Switch>
</>
)}
</>
); );
}; };