From d464a8a255fee2f051005148498c081cc2dbe4c8 Mon Sep 17 00:00:00 2001 From: Chirag Madlani <12962843+chirag-madlani@users.noreply.github.com> Date: Tue, 31 Jan 2023 00:47:39 +0530 Subject: [PATCH] fix(ui): validate token expiry on tab focus (#10011) * fix(ui): validate token expiry on tab focus * show toast message if token is expired --- .../ui/src/components/app-bar/Appbar.tsx | 24 +++++++++++++++++++ .../ui/src/locale/languages/en-us.json | 1 + 2 files changed, 25 insertions(+) diff --git a/openmetadata-ui/src/main/resources/ui/src/components/app-bar/Appbar.tsx b/openmetadata-ui/src/main/resources/ui/src/components/app-bar/Appbar.tsx index 6637b371098..adefd0873c0 100644 --- a/openmetadata-ui/src/main/resources/ui/src/components/app-bar/Appbar.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/components/app-bar/Appbar.tsx @@ -19,7 +19,9 @@ import { observer } from 'mobx-react'; import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Link, useHistory, useLocation, useRouteMatch } from 'react-router-dom'; +import { toast } from 'react-toastify'; import { getVersion } from 'rest/miscAPI'; +import { extractDetailsFromToken } from 'utils/AuthProvider.util'; import appState from '../../AppState'; import { getExplorePathWithSearch, @@ -336,6 +338,28 @@ const Appbar: React.FC = (): JSX.Element => { } }, [appState.userDetails, isAuthDisabled]); + useEffect(() => { + const handleDocumentVisibilityChange = () => { + if ( + isProtectedRoute(location.pathname) && + isTourRoute(location.pathname) + ) { + return; + } + const { isExpired, exp } = extractDetailsFromToken(); + if (!document.hidden && isExpired) { + exp && toast.info(t('message.session-expired')); + onLogoutHandler(); + } + }; + + addEventListener('focus', handleDocumentVisibilityChange); + + return () => { + removeEventListener('focus', handleDocumentVisibilityChange); + }; + }, []); + return ( <> {isProtectedRoute(location.pathname) && diff --git a/openmetadata-ui/src/main/resources/ui/src/locale/languages/en-us.json b/openmetadata-ui/src/main/resources/ui/src/locale/languages/en-us.json index 1f617906300..425425e2679 100644 --- a/openmetadata-ui/src/main/resources/ui/src/locale/languages/en-us.json +++ b/openmetadata-ui/src/main/resources/ui/src/locale/languages/en-us.json @@ -788,6 +788,7 @@ "select-team": "Please select a team type", "select-token-expiration": "Select Token Expiration", "service-created-entity-description": "The has been created successfully. Visit the newly created service to take a look at the details. {{entity}}", + "session-expired": "Your session has timed out! Please sign in again to access OpenMetadata.", "something-went-wrong": "Something went wrong", "special-character-not-allowed": "Special characters are not allowed.", "sql-query-tooltip": "Queries returning one or more rows will result in the test failing.",