From 4f04dff6cefeec5f10b8bc20df9a2d632fe4f509 Mon Sep 17 00:00:00 2001 From: Sachin Chaurasiya Date: Thu, 18 Apr 2024 18:22:34 +0530 Subject: [PATCH] ui: improve refresh token usage part 2 (#15947) * ui: improve refresh token usage part 2 * Fix loading state issue in AuthProvider component --- .../resources/ui/src/components/AppBar/Appbar.tsx | 11 ++++------- .../components/Auth/AuthProviders/AuthProvider.tsx | 8 ++++---- .../resources/ui/src/hooks/useApplicationStore.ts | 5 +++++ .../resources/ui/src/interface/store.interface.ts | 2 ++ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/openmetadata-ui/src/main/resources/ui/src/components/AppBar/Appbar.tsx b/openmetadata-ui/src/main/resources/ui/src/components/AppBar/Appbar.tsx index 78f67a1a4f2b..9f0ca22c9d4a 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/AppBar/Appbar.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/AppBar/Appbar.tsx @@ -14,9 +14,7 @@ import { isString } from 'lodash'; import Qs from 'qs'; import React, { useEffect, useState } from 'react'; -import { useTranslation } from 'react-i18next'; import { useHistory, useLocation } from 'react-router-dom'; -import { toast } from 'react-toastify'; import { getExplorePath, TOUR_SEARCH_TERM } from '../../constants/constants'; import { useTourProvider } from '../../context/TourProvider/TourProvider'; import { CurrentTourPageType } from '../../enums/tour.enum'; @@ -35,11 +33,10 @@ const Appbar: React.FC = (): JSX.Element => { const tabsInfo = searchClassBase.getTabsInfo(); const location = useLocation(); const history = useHistory(); - const { t } = useTranslation(); const { isTourOpen, updateTourPage, updateTourSearch, tourSearchValue } = useTourProvider(); - const { isAuthenticated, searchCriteria, onLogoutHandler, getOidcToken } = + const { isAuthenticated, searchCriteria, getOidcToken, trySilentSignIn } = useApplicationStore(); const parsedQueryString = Qs.parse( @@ -124,10 +121,10 @@ const Appbar: React.FC = (): JSX.Element => { ) { return; } - const { isExpired, exp } = extractDetailsFromToken(getOidcToken()); + const { isExpired } = extractDetailsFromToken(getOidcToken()); if (!document.hidden && isExpired) { - exp && toast.info(t('message.session-expired')); - onLogoutHandler(); + // force logout + trySilentSignIn(true); } }; diff --git a/openmetadata-ui/src/main/resources/ui/src/components/Auth/AuthProviders/AuthProvider.tsx b/openmetadata-ui/src/main/resources/ui/src/components/Auth/AuthProviders/AuthProvider.tsx index 16884f766e3d..7c8d5bb10a5a 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/Auth/AuthProviders/AuthProvider.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/Auth/AuthProviders/AuthProvider.tsx @@ -68,7 +68,7 @@ import { isProtectedRoute, } from '../../../utils/AuthProvider.util'; import { escapeESReservedCharacters } from '../../../utils/StringsUtils'; -import { showErrorToast } from '../../../utils/ToastUtils'; +import { showErrorToast, showInfoToast } from '../../../utils/ToastUtils'; import { getUserDataFromOidc, matchUserDetails, @@ -195,6 +195,7 @@ export const AuthProvider = ({ clearTimeout(timeoutId); if (forceLogout) { onLogoutHandler(); + showInfoToast(t('message.session-expired')); } else { history.push(ROUTES.SIGNIN); } @@ -280,7 +281,7 @@ export const AuthProvider = ({ if ([ROUTES.SIGNIN, ROUTES.SIGNUP].includes(pathName)) { return; } - setLoading(true); + try { // Try to renew token const newToken = await renewIdToken(); @@ -304,8 +305,6 @@ export const AuthProvider = ({ } // reset user details if silent signIn fails resetUserDetails(forceLogout); - } finally { - setLoading(false); } }; @@ -673,6 +672,7 @@ export const AuthProvider = ({ onLoginHandler, onLogoutHandler, handleSuccessfulLogin, + trySilentSignIn, handleFailedLogin, updateAxiosInterceptors: initializeAxiosInterceptors, }); diff --git a/openmetadata-ui/src/main/resources/ui/src/hooks/useApplicationStore.ts b/openmetadata-ui/src/main/resources/ui/src/hooks/useApplicationStore.ts index ed70ee83e39b..3b8b4ca4bd35 100644 --- a/openmetadata-ui/src/main/resources/ui/src/hooks/useApplicationStore.ts +++ b/openmetadata-ui/src/main/resources/ui/src/hooks/useApplicationStore.ts @@ -101,6 +101,11 @@ export const useApplicationStore = create()( updateAxiosInterceptors: () => { // This is a placeholder function that will be replaced by the actual function }, + trySilentSignIn: (forceLogout?: boolean) => { + if (forceLogout) { + // This is a placeholder function that will be replaced by the actual function + } + }, updateCurrentUser: (user) => { set({ currentUser: user }); }, diff --git a/openmetadata-ui/src/main/resources/ui/src/interface/store.interface.ts b/openmetadata-ui/src/main/resources/ui/src/interface/store.interface.ts index f22787cc35ea..7f299d5a1fb0 100644 --- a/openmetadata-ui/src/main/resources/ui/src/interface/store.interface.ts +++ b/openmetadata-ui/src/main/resources/ui/src/interface/store.interface.ts @@ -35,6 +35,7 @@ export interface HelperFunctions { handleSuccessfulLogin: (user: OidcUser) => Promise; handleFailedLogin: () => void; updateAxiosInterceptors: () => void; + trySilentSignIn: (forceLogout?: boolean) => Promise; } export interface ApplicationStore @@ -74,6 +75,7 @@ export interface ApplicationStore removeOidcToken: () => void; removeRefreshToken: () => void; updateSearchCriteria: (criteria: ExploreSearchIndex | '') => void; + trySilentSignIn: (forceLogout?: boolean) => void; } export interface DomainStore {