mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-07-31 21:02:00 +08:00
Auth Wrapper is updated (#1028)
This commit is contained in:
parent
d52308c9b5
commit
c81b0b2a8b
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
import { notification } from 'antd';
|
import { notification } from 'antd';
|
||||||
import getLocalStorageApi from 'api/browser/localstorage/get';
|
import getLocalStorageApi from 'api/browser/localstorage/get';
|
||||||
import loginApi from 'api/user/login';
|
import loginApi from 'api/user/login';
|
||||||
@ -8,7 +9,7 @@ import history from 'lib/history';
|
|||||||
import React, { useEffect, useMemo } from 'react';
|
import React, { useEffect, useMemo } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useDispatch, useSelector } from 'react-redux';
|
import { useDispatch, useSelector } from 'react-redux';
|
||||||
import { matchPath, Redirect } from 'react-router-dom';
|
import { matchPath, Redirect, useLocation } from 'react-router-dom';
|
||||||
import { Dispatch } from 'redux';
|
import { Dispatch } from 'redux';
|
||||||
import { AppState } from 'store/reducers';
|
import { AppState } from 'store/reducers';
|
||||||
import { getInitialUserTokenRefreshToken } from 'store/utils';
|
import { getInitialUserTokenRefreshToken } from 'store/utils';
|
||||||
@ -21,43 +22,56 @@ import routes from './routes';
|
|||||||
import afterLogin from './utils';
|
import afterLogin from './utils';
|
||||||
|
|
||||||
function PrivateRoute({ children }: PrivateRouteProps): JSX.Element {
|
function PrivateRoute({ children }: PrivateRouteProps): JSX.Element {
|
||||||
|
const { pathname } = useLocation();
|
||||||
|
|
||||||
const mapRoutes = useMemo(
|
const mapRoutes = useMemo(
|
||||||
() =>
|
() =>
|
||||||
new Map(
|
new Map(
|
||||||
routes.map((e) => {
|
routes.map((e) => {
|
||||||
const currentPath = matchPath(history.location.pathname, {
|
const currentPath = matchPath(pathname, {
|
||||||
path: e.path,
|
path: e.path,
|
||||||
});
|
});
|
||||||
return [currentPath === null ? null : 'current', e];
|
return [currentPath === null ? null : 'current', e];
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
[],
|
[pathname],
|
||||||
);
|
);
|
||||||
const { isUserFetching, isUserFetchingError } = useSelector<
|
const {
|
||||||
AppState,
|
isUserFetching,
|
||||||
AppReducer
|
isUserFetchingError,
|
||||||
>((state) => state.app);
|
isLoggedIn: isLoggedInState,
|
||||||
|
} = useSelector<AppState, AppReducer>((state) => state.app);
|
||||||
|
|
||||||
const { t } = useTranslation(['common']);
|
const { t } = useTranslation(['common']);
|
||||||
|
|
||||||
const currentRoute = mapRoutes.get('current');
|
|
||||||
const dispatch = useDispatch<Dispatch<AppActions>>();
|
const dispatch = useDispatch<Dispatch<AppActions>>();
|
||||||
|
|
||||||
const isLoggedIn = getLocalStorageApi(LOCALSTORAGE.IS_LOGGED_IN);
|
const currentRoute = mapRoutes.get('current');
|
||||||
|
|
||||||
|
const navigateToLoginIfNotLoggedIn = (isLoggedIn = isLoggedInState): void => {
|
||||||
|
dispatch({
|
||||||
|
type: UPDATE_USER_IS_FETCH,
|
||||||
|
payload: {
|
||||||
|
isUserFetching: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!isLoggedIn) {
|
||||||
|
history.push(ROUTES.LOGIN);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// eslint-disable-next-line sonarjs/cognitive-complexity
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async (): Promise<void> => {
|
(async (): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
console.log('asdasd');
|
|
||||||
|
|
||||||
if (currentRoute) {
|
if (currentRoute) {
|
||||||
const { isPrivate, key } = currentRoute;
|
const { isPrivate, key } = currentRoute;
|
||||||
|
|
||||||
if (isPrivate) {
|
if (isPrivate) {
|
||||||
const localStorageUserAuthToken = getInitialUserTokenRefreshToken();
|
const localStorageUserAuthToken = getInitialUserTokenRefreshToken();
|
||||||
|
|
||||||
if (isLoggedIn) {
|
if (!isLoggedInState) {
|
||||||
if (localStorageUserAuthToken && localStorageUserAuthToken.refreshJwt) {
|
if (localStorageUserAuthToken && localStorageUserAuthToken.refreshJwt) {
|
||||||
// localstorage token is present
|
// localstorage token is present
|
||||||
const { refreshJwt } = localStorageUserAuthToken;
|
const { refreshJwt } = localStorageUserAuthToken;
|
||||||
@ -89,7 +103,7 @@ function PrivateRoute({ children }: PrivateRouteProps): JSX.Element {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// user is not logged in
|
// user does have localstorage values
|
||||||
dispatch({
|
dispatch({
|
||||||
type: UPDATE_USER_IS_FETCH,
|
type: UPDATE_USER_IS_FETCH,
|
||||||
payload: {
|
payload: {
|
||||||
@ -99,14 +113,7 @@ function PrivateRoute({ children }: PrivateRouteProps): JSX.Element {
|
|||||||
history.push(ROUTES.LOGIN);
|
history.push(ROUTES.LOGIN);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dispatch({
|
navigateToLoginIfNotLoggedIn();
|
||||||
type: UPDATE_USER_IS_FETCH,
|
|
||||||
payload: {
|
|
||||||
isUserFetching: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
history.push(ROUTES.LOGIN);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// no need to fetch the user and make user fetching false
|
// no need to fetch the user and make user fetching false
|
||||||
@ -117,39 +124,25 @@ function PrivateRoute({ children }: PrivateRouteProps): JSX.Element {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (history.location.pathname === ROUTES.HOME_PAGE) {
|
} else if (pathname === ROUTES.HOME_PAGE) {
|
||||||
// routing to application page over root page
|
// routing to application page over root page
|
||||||
if (isLoggedIn) {
|
if (isLoggedInState) {
|
||||||
history.push(ROUTES.APPLICATION);
|
history.push(ROUTES.APPLICATION);
|
||||||
} else {
|
} else {
|
||||||
dispatch({
|
navigateToLoginIfNotLoggedIn();
|
||||||
type: UPDATE_USER_IS_FETCH,
|
|
||||||
payload: {
|
|
||||||
isUserFetching: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
history.push(ROUTES.LOGIN);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dispatch({
|
// not found
|
||||||
type: UPDATE_USER_IS_FETCH,
|
navigateToLoginIfNotLoggedIn(
|
||||||
payload: {
|
getLocalStorageApi(LOCALSTORAGE.IS_LOGGED_IN) === 'true',
|
||||||
isUserFetching: false,
|
);
|
||||||
},
|
|
||||||
});
|
|
||||||
if (!isLoggedIn) {
|
|
||||||
history.push(ROUTES.LOGIN);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// something went wrong
|
// something went wrong
|
||||||
history.push(ROUTES.SOMETHING_WENT_WRONG);
|
history.push(ROUTES.SOMETHING_WENT_WRONG);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
// need to run over mount only
|
}, [dispatch, isLoggedInState, currentRoute]);
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [dispatch, currentRoute, isLoggedIn]);
|
|
||||||
|
|
||||||
if (isUserFetchingError) {
|
if (isUserFetchingError) {
|
||||||
return <Redirect to={ROUTES.SOMETHING_WENT_WRONG} />;
|
return <Redirect to={ROUTES.SOMETHING_WENT_WRONG} />;
|
||||||
|
@ -1,20 +1,11 @@
|
|||||||
import { Button, Typography } from 'antd';
|
import { Button, Typography } from 'antd';
|
||||||
import getLocalStorageKey from 'api/browser/localstorage/get';
|
|
||||||
import SomethingWentWrongAsset from 'assets/SomethingWentWrong';
|
import SomethingWentWrongAsset from 'assets/SomethingWentWrong';
|
||||||
import { Container } from 'components/NotFound/styles';
|
import { Container } from 'components/NotFound/styles';
|
||||||
import { LOCALSTORAGE } from 'constants/localStorage';
|
|
||||||
import ROUTES from 'constants/routes';
|
import ROUTES from 'constants/routes';
|
||||||
import history from 'lib/history';
|
import history from 'lib/history';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useDispatch } from 'react-redux';
|
|
||||||
import { Dispatch } from 'redux';
|
|
||||||
import AppActions from 'types/actions';
|
|
||||||
import { LOGGED_IN } from 'types/actions/app';
|
|
||||||
|
|
||||||
function SomethingWentWrong(): JSX.Element {
|
function SomethingWentWrong(): JSX.Element {
|
||||||
const dispatch = useDispatch<Dispatch<AppActions>>();
|
|
||||||
const isLoggedIn = getLocalStorageKey(LOCALSTORAGE.IS_LOGGED_IN);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container>
|
<Container>
|
||||||
<SomethingWentWrongAsset />
|
<SomethingWentWrongAsset />
|
||||||
@ -22,15 +13,6 @@ function SomethingWentWrong(): JSX.Element {
|
|||||||
<Button
|
<Button
|
||||||
type="primary"
|
type="primary"
|
||||||
onClick={(): void => {
|
onClick={(): void => {
|
||||||
if (isLoggedIn) {
|
|
||||||
dispatch({
|
|
||||||
type: LOGGED_IN,
|
|
||||||
payload: {
|
|
||||||
isLoggedIn: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
history.push(ROUTES.APPLICATION);
|
history.push(ROUTES.APPLICATION);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user