From 084d8ecccdeacaeb234246b70b4483dfcc60aa6f Mon Sep 17 00:00:00 2001 From: palash-signoz Date: Thu, 5 May 2022 21:21:39 +0530 Subject: [PATCH] chore: logout is updated (#1100) --- frontend/src/AppRoutes/utils.ts | 10 +---- frontend/src/api/utils.ts | 46 ++++++++++++++++----- frontend/src/container/Header/index.tsx | 53 ++----------------------- 3 files changed, 42 insertions(+), 67 deletions(-) diff --git a/frontend/src/AppRoutes/utils.ts b/frontend/src/AppRoutes/utils.ts index 5f3405ceea..698629d325 100644 --- a/frontend/src/AppRoutes/utils.ts +++ b/frontend/src/AppRoutes/utils.ts @@ -1,11 +1,8 @@ -import { notification } from 'antd'; import getLocalStorageApi from 'api/browser/localstorage/get'; import setLocalStorageApi from 'api/browser/localstorage/set'; import getUserApi from 'api/user/getUser'; +import { Logout } from 'api/utils'; import { LOCALSTORAGE } from 'constants/localStorage'; -import ROUTES from 'constants/routes'; -import { t } from 'i18next'; -import history from 'lib/history'; import store from 'store'; import AppActions from 'types/actions'; import { @@ -86,11 +83,8 @@ const afterLogin = async ( }, }); - notification.error({ - message: getUserResponse.error || t('something_went_wrong'), - }); + Logout(); - history.push(ROUTES.SOMETHING_WENT_WRONG); return undefined; }; diff --git a/frontend/src/api/utils.ts b/frontend/src/api/utils.ts index 3e3d720477..56867927a8 100644 --- a/frontend/src/api/utils.ts +++ b/frontend/src/api/utils.ts @@ -3,19 +3,17 @@ import { LOCALSTORAGE } from 'constants/localStorage'; import ROUTES from 'constants/routes'; import history from 'lib/history'; import store from 'store'; -import { LOGGED_IN, UPDATE_USER_IS_FETCH } from 'types/actions/app'; +import { + LOGGED_IN, + UPDATE_USER, + UPDATE_USER_ACCESS_REFRESH_ACCESS_TOKEN, + UPDATE_USER_ORG_ROLE, +} from 'types/actions/app'; export const Logout = (): void => { - deleteLocalStorageKey(LOCALSTORAGE.REFRESH_AUTH_TOKEN); deleteLocalStorageKey(LOCALSTORAGE.AUTH_TOKEN); deleteLocalStorageKey(LOCALSTORAGE.IS_LOGGED_IN); - - store.dispatch({ - type: UPDATE_USER_IS_FETCH, - payload: { - isUserFetching: false, - }, - }); + deleteLocalStorageKey(LOCALSTORAGE.REFRESH_AUTH_TOKEN); store.dispatch({ type: LOGGED_IN, @@ -24,6 +22,34 @@ export const Logout = (): void => { }, }); - // navigate to login + store.dispatch({ + type: UPDATE_USER_ORG_ROLE, + payload: { + org: null, + role: null, + }, + }); + + store.dispatch({ + type: UPDATE_USER, + payload: { + ROLE: 'VIEWER', + email: '', + name: '', + orgId: '', + orgName: '', + profilePictureURL: '', + userId: '', + }, + }); + + store.dispatch({ + type: UPDATE_USER_ACCESS_REFRESH_ACCESS_TOKEN, + payload: { + accessJwt: '', + refreshJwt: '', + }, + }); + history.push(ROUTES.LOGIN); }; diff --git a/frontend/src/container/Header/index.tsx b/frontend/src/container/Header/index.tsx index d3f1436f9a..c3c9eb8e84 100644 --- a/frontend/src/container/Header/index.tsx +++ b/frontend/src/container/Header/index.tsx @@ -12,25 +12,17 @@ import { Space, Typography, } from 'antd'; -import remove from 'api/browser/localstorage/remove'; -import { LOCALSTORAGE } from 'constants/localStorage'; +import { Logout } from 'api/utils'; import ROUTES from 'constants/routes'; -import history from 'lib/history'; import setTheme, { AppMode } from 'lib/theme/setTheme'; import React, { useCallback, useState } from 'react'; -import { connect, useDispatch, useSelector } from 'react-redux'; +import { connect, useSelector } from 'react-redux'; import { NavLink } from 'react-router-dom'; -import { bindActionCreators, Dispatch } from 'redux'; +import { bindActionCreators } from 'redux'; import { ThunkDispatch } from 'redux-thunk'; import { ToggleDarkMode } from 'store/actions'; import { AppState } from 'store/reducers'; import AppActions from 'types/actions'; -import { - LOGGED_IN, - UPDATE_USER, - UPDATE_USER_ACCESS_REFRESH_ACCESS_TOKEN, - UPDATE_USER_ORG_ROLE, -} from 'types/actions/app'; import AppReducer from 'types/reducer/app'; import CurrentOrganization from './CurrentOrganization'; @@ -47,7 +39,6 @@ function HeaderContainer({ toggleDarkMode }: Props): JSX.Element { (state) => state.app, ); const [isUserDropDownOpen, setIsUserDropDownOpen] = useState(); - const dispatch = useDispatch>(); const onToggleThemeHandler = useCallback(() => { const preMode: AppMode = isDarkMode ? 'lightMode' : 'darkMode'; @@ -75,43 +66,7 @@ function HeaderContainer({ toggleDarkMode }: Props): JSX.Element { }; const onClickLogoutHandler = (): void => { - remove(LOCALSTORAGE.AUTH_TOKEN); - remove(LOCALSTORAGE.IS_LOGGED_IN); - remove(LOCALSTORAGE.REFRESH_AUTH_TOKEN); - dispatch({ - type: LOGGED_IN, - payload: { - isLoggedIn: false, - }, - }); - dispatch({ - type: UPDATE_USER_ORG_ROLE, - payload: { - org: null, - role: null, - }, - }); - dispatch({ - type: UPDATE_USER, - payload: { - ROLE: 'VIEWER', - email: '', - name: '', - orgId: '', - orgName: '', - profilePictureURL: '', - userId: '', - }, - }); - dispatch({ - type: UPDATE_USER_ACCESS_REFRESH_ACCESS_TOKEN, - payload: { - accessJwt: '', - refreshJwt: '', - }, - }); - - history.push(ROUTES.LOGIN); + Logout(); }; const menu = (