Skip to content

Commit

Permalink
feat: make identity call if user is logs in for first time or if iden…
Browse files Browse the repository at this point in the history
…tity call was not registered
  • Loading branch information
YounixM committed Sep 23, 2023
1 parent 043e5ca commit 2df1682
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
19 changes: 17 additions & 2 deletions frontend/src/AppRoutes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ConfigProvider } from 'antd';
import getLocalStorageApi from 'api/browser/localstorage/get';
import setLocalStorageApi from 'api/browser/localstorage/set';
import NotFound from 'components/NotFound';
import Spinner from 'components/Spinner';
import { FeatureKeys } from 'constants/features';
import { LOCALSTORAGE } from 'constants/localStorage';
import ROUTES from 'constants/routes';
import AppLayout from 'container/AppLayout';
import { useThemeConfig } from 'hooks/useDarkMode';
Expand Down Expand Up @@ -75,14 +78,26 @@ function App(): JSX.Element {
});

useEffect(() => {
if (isLoggedInState && user && user.userId && user.email) {
const isIdentifiedUser = getLocalStorageApi(LOCALSTORAGE.IS_IDENTIFIED_USER);

if (
isLoggedInState &&
user &&
user.userId &&
user.email &&
!isIdentifiedUser
) {
console.log('Use Effect called', isLoggedInState, user);

setLocalStorageApi(LOCALSTORAGE.IS_IDENTIFIED_USER, 'true');

window.analytics.identify(user?.email, {
email: user?.email,
name: user?.name,
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoggedInState]);
}, [isLoggedInState, user]);

useEffect(() => {
trackPageView(pathname);
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
export const Logout = (): void => {
deleteLocalStorageKey(LOCALSTORAGE.AUTH_TOKEN);
deleteLocalStorageKey(LOCALSTORAGE.IS_LOGGED_IN);
deleteLocalStorageKey(LOCALSTORAGE.IS_IDENTIFIED_USER);
deleteLocalStorageKey(LOCALSTORAGE.REFRESH_AUTH_TOKEN);
deleteLocalStorageKey(LOCALSTORAGE.LOGGED_IN_USER_EMAIL);
deleteLocalStorageKey(LOCALSTORAGE.LOGGED_IN_USER_NAME);
deleteLocalStorageKey(LOCALSTORAGE.CHAT_SUPPORT);

store.dispatch({
type: LOGGED_IN,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/constants/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export enum LOCALSTORAGE {
LOGGED_IN_USER_NAME = 'LOGGED_IN_USER_NAME',
LOGGED_IN_USER_EMAIL = 'LOGGED_IN_USER_EMAIL',
CHAT_SUPPORT = 'CHAT_SUPPORT',
IS_IDENTIFIED_USER = 'IS_IDENTIFIED_USER',
}
18 changes: 1 addition & 17 deletions frontend/src/container/Login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Button, Form, Input, Space, Tooltip, Typography } from 'antd';
import setLocalStorageApi from 'api/browser/localstorage/set';
import getUserVersion from 'api/user/getVersion';
import loginApi from 'api/user/login';
import loginPrecheckApi from 'api/user/loginPrecheck';
import afterLogin from 'AppRoutes/utils';
import { FeatureKeys } from 'constants/features';
import { LOCALSTORAGE } from 'constants/localStorage';
import ROUTES from 'constants/routes';
import useFeatureFlag from 'hooks/useFeatureFlag';
import { useNotifications } from 'hooks/useNotifications';
import history from 'lib/history';
import { useEffect, useState } from 'react';
Expand Down Expand Up @@ -42,9 +38,6 @@ function Login({
const { t } = useTranslation(['login']);
const [isLoading, setIsLoading] = useState<boolean>(false);
const { user } = useSelector<AppState, AppReducer>((state) => state.app);
const isChatSupportEnabled: boolean | undefined = useFeatureFlag(
FeatureKeys.CHAT_SUPPORT,
)?.active;

const [precheckResult, setPrecheckResult] = useState<PrecheckResultType>({
sso: false,
Expand Down Expand Up @@ -165,21 +158,12 @@ function Login({
password,
});
if (response.statusCode === 200) {
const user = await afterLogin(
await afterLogin(
response.payload.userId,
response.payload.accessJwt,
response.payload.refreshJwt,
);

if (user) {
setLocalStorageApi(LOCALSTORAGE.LOGGED_IN_USER_NAME, user.payload?.name);
setLocalStorageApi(LOCALSTORAGE.LOGGED_IN_USER_EMAIL, user.payload?.email);
setLocalStorageApi(
LOCALSTORAGE.CHAT_SUPPORT,
(isChatSupportEnabled || '').toString(),
);
}

history.push(ROUTES.APPLICATION);
} else {
notifications.error({
Expand Down

0 comments on commit 2df1682

Please sign in to comment.