Skip to content

Commit

Permalink
ui: improve refresh token usage part 2 (#15947)
Browse files Browse the repository at this point in the history
* ui: improve refresh token usage part 2

* Fix loading state issue in AuthProvider component
  • Loading branch information
Sachin-chaurasiya authored and ulixius9 committed Apr 19, 2024
1 parent ab7866d commit 4f04dff
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(
Expand Down Expand Up @@ -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);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -195,6 +195,7 @@ export const AuthProvider = ({
clearTimeout(timeoutId);
if (forceLogout) {
onLogoutHandler();
showInfoToast(t('message.session-expired'));
} else {
history.push(ROUTES.SIGNIN);
}
Expand Down Expand Up @@ -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();
Expand All @@ -304,8 +305,6 @@ export const AuthProvider = ({
}
// reset user details if silent signIn fails
resetUserDetails(forceLogout);
} finally {
setLoading(false);
}
};

Expand Down Expand Up @@ -673,6 +672,7 @@ export const AuthProvider = ({
onLoginHandler,
onLogoutHandler,
handleSuccessfulLogin,
trySilentSignIn,
handleFailedLogin,
updateAxiosInterceptors: initializeAxiosInterceptors,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ export const useApplicationStore = create<ApplicationStore>()(
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 });
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface HelperFunctions {
handleSuccessfulLogin: (user: OidcUser) => Promise<void>;
handleFailedLogin: () => void;
updateAxiosInterceptors: () => void;
trySilentSignIn: (forceLogout?: boolean) => Promise<void>;
}

export interface ApplicationStore
Expand Down Expand Up @@ -74,6 +75,7 @@ export interface ApplicationStore
removeOidcToken: () => void;
removeRefreshToken: () => void;
updateSearchCriteria: (criteria: ExploreSearchIndex | '') => void;
trySilentSignIn: (forceLogout?: boolean) => void;
}

export interface DomainStore {
Expand Down

0 comments on commit 4f04dff

Please sign in to comment.