Open ajitesh123 opened 1 day ago
The issue here is that the frontend application is making unnecessary login requests on page refresh, which is causing performance and user experience problems. This could be due to the way the application is handling user authentication and session management.
Possible reasons for this behavior could be:
The frontend application is not properly managing the user's authentication state and login status.
The frontend application is not caching the user's login status or session information, leading to repeated login requests on page refresh.
The frontend application is not properly handling the response from the login endpoint, leading to repeated login attempts.
There could be an issue with the way the backend is handling user authentication and session management, leading to the frontend making unnecessary login requests.
To address this issue, the junior developer should follow these steps:
Analyze the frontend application's authentication and session management logic:
Implement caching for user authentication and session information:
Optimize the login request handling:
Coordinate with the backend team:
Test and validate the changes:
path: ee/query-service/sso/saml/request.go objective: REFER instruction: - Review this file to understand how the backend is handling SAML authentication requests.
path: frontend/src/container/TopNav/DateTimeSelection/Refresh.tsx objective: REFER_AND_UPDATE instruction: - Review this file to understand how the frontend is handling the page refresh functionality and login requests.
path: frontend/src/container/TopNav/DateTimeSelectionV2/Refresh.tsx objective: REFER_AND_UPDATE instruction: - Review this file to understand how the frontend is handling the page refresh functionality and login requests.
path: frontend/src/api/browser/localstorage/get.ts objective: REFER_AND_UPDATE instruction: - Review this file to understand how the frontend is using the browser's localStorage to retrieve cached data.
path: frontend/src/api/browser/localstorage/set.ts objective: REFER_AND_UPDATE instruction: - Review this file to understand how the frontend is using the browser's localStorage to store cached data.
--- frontend/src/container/TopNav/DateTimeSelection/Refresh.tsx
+++ frontend/src/container/TopNav/DateTimeSelection/Refresh.tsx
@@ -1,5 +1,7 @@
import { useEffect, useState } from 'react';
+import { useSelector } from 'react-redux';
+import { getFromLocalStorage, setToLocalStorage } from '../../../api/browser/localstorage';
import { RefreshTextContainer, Typography } from './styles';
function RefreshText({
@@ -7,6 +9,16 @@ function RefreshText({
refreshButtonHidden,
}: RefreshTextProps): JSX.Element {
const [refreshText, setRefreshText] = useState<string>('');
+ const isLoggedIn = useSelector((state: any) => state.auth.isLoggedIn);
+
+ useEffect(() => {
+ // Check if user is already logged in and session is valid
+ const cachedSessionInfo = getFromLocalStorage('sessionInfo');
+ if (!isLoggedIn && !cachedSessionInfo) {
+ // Make login request if user is not logged in and session is not valid
+ handleLogin();
+ }
+ }, [isLoggedIn]);
// this is to update the refresh text
useEffect(() => {
@@ -20,6 +32,18 @@ function RefreshText({
};
}, [onLastRefreshHandler, refreshText]);
+ const handleLogin = async () => {
+ try {
+ // Make login request and get session information
+ const sessionInfo = await loginRequest();
+ // Cache the session information in localStorage
+ setToLocalStorage('sessionInfo', JSON.stringify(sessionInfo));
+ // Update the application state with the logged in status and session information
+ } catch (error) {
+ // Handle login error
+ }
+ };
+
return (
<RefreshTextContainer refreshButtonHidden={refreshButtonHidden}>
<Typography>{refreshText}</Typography>
@@ -31,4 +55,4 @@ interface RefreshTextProps {
refreshButtonHidden: boolean;
}
export default RefreshText;
--- frontend/src/container/TopNav/DateTimeSelectionV2/Refresh.tsx
+++ frontend/src/container/TopNav/DateTimeSelectionV2/Refresh.tsx
@@ -1,5 +1,7 @@
import { useEffect, useState } from 'react';
+import { useSelector } from 'react-redux';
+import { getFromLocalStorage, setToLocalStorage } from '../../../api/browser/localstorage';
import { RefreshTextContainer, Typography } from './styles';
function RefreshText({
@@ -7,6 +9,16 @@ function RefreshText({
refreshButtonHidden,
}: RefreshTextProps): JSX.Element {
const [refreshText, setRefreshText] = useState<string>('');
+ const isLoggedIn = useSelector((state: any) => state.auth.isLoggedIn);
+
+ useEffect(() => {
+ // Check if user is already logged in and session is valid
+ const cachedSessionInfo = getFromLocalStorage('sessionInfo');
+ if (!isLoggedIn && !cachedSessionInfo) {
+ // Make login request if user is not logged in and session is not valid
+ handleLogin();
+ }
+ }, [isLoggedIn]);
// this is to update the refresh text
useEffect(() => {
@@ -20,6 +32,18 @@ function RefreshText({
};
}, [onLastRefreshHandler, refreshText]);
+ const handleLogin = async () => {
+ try {
+ // Make login request and get session information
+ const sessionInfo = await loginRequest();
+ // Cache the session information in localStorage
+ setToLocalStorage('sessionInfo', JSON.stringify(sessionInfo));
+ // Update the application state with the logged in status and session information
+ } catch (error) {
+ // Handle login error
+ }
+ };
+
return (
<RefreshTextContainer refreshButtonHidden={refreshButtonHidden}>
<Typography>{refreshText}</Typography>
@@ -31,4 +55,4 @@ interface RefreshTextProps {
refreshButtonHidden: boolean;
}
export default RefreshText;
--- frontend/src/api/browser/localstorage/get.ts
+++ frontend/src/api/browser/localstorage/get.ts
@@ -1,9 +1,14 @@
-const get = (key: string): string | null => {
+const getFromLocalStorage = (key: string): any => {
try {
- return localStorage.getItem(key);
+ const value = localStorage.getItem(key);
+ if (value) {
+ return JSON.parse(value);
+ }
+ return null;
} catch (e) {
- return '';
+ console.error('Error getting data from localStorage:', e);
+ return null;
}
};
-export default get;
+export default getFromLocalStorage;
--- frontend/src/api/browser/localstorage/set.ts
+++ frontend/src/api/browser/localstorage/set.ts
@@ -1,10 +1,13 @@
-const set = (key: string, value: string): boolean => {
+const setToLocalStorage = (key: string, value: string): boolean => {
try {
- localStorage.setItem(key, value);
+ localStorage.setItem(key, JSON.stringify(value));
return true;
} catch (e) {
+ console.error('Error setting data in localStorage:', e);
return false;
}
};
-export default set;
+export {
+ setToLocalStorage,
+};
On page refresh frontend makes unnecessary login requests