summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Shetye2025-03-11 17:47:16 +0530
committerSunil Shetye2025-03-11 17:47:16 +0530
commitff0ee3ae99708eaf6f8db8361423cc32a974db34 (patch)
tree1d73e70be03eb3a81ff2b625f1d7c9a2d1814297
parent70e28ed8402e16f4d3caeda5449f5001834a1985 (diff)
downloadCommon-Interface-Project-ff0ee3ae99708eaf6f8db8361423cc32a974db34.tar.gz
Common-Interface-Project-ff0ee3ae99708eaf6f8db8361423cc32a974db34.tar.bz2
Common-Interface-Project-ff0ee3ae99708eaf6f8db8361423cc32a974db34.zip
moving session refresh code to Api.js
-rw-r--r--blocks/eda-frontend/src/App.js30
-rw-r--r--blocks/eda-frontend/src/utils/Api.js36
2 files changed, 34 insertions, 32 deletions
diff --git a/blocks/eda-frontend/src/App.js b/blocks/eda-frontend/src/App.js
index e5bc6ff8..9d72db82 100644
--- a/blocks/eda-frontend/src/App.js
+++ b/blocks/eda-frontend/src/App.js
@@ -1,7 +1,6 @@
import React, { useEffect } from 'react'
import PropTypes from 'prop-types'
import { HashRouter, Switch, Route, Redirect } from 'react-router-dom'
-import api from './utils/Api'
import CircularProgress from '@material-ui/core/CircularProgress'
import Navbar from './components/Shared/Navbar'
@@ -76,36 +75,7 @@ PublicRoute.propTypes = {
restricted: PropTypes.bool
}
-const deleteCookie = (name) => {
- document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'
-}
-
const App = () => {
- useEffect(() => {
- const fetchSession = async () => {
- // Check if session ID already exists
- let sessionId = localStorage.getItem('session_id')
- let expireAt = localStorage.getItem('expire_at')
-
- if (!sessionId || !expireAt || new Date(expireAt) < new Date()) {
- // Generate a new session ID
- localStorage.removeItem('session_id')
- localStorage.removeItem('expire_at')
- deleteCookie('sessionid')
- api.get('simulation/get_session?app_name=' + process.env.REACT_APP_NAME)
- .then(res => {
- sessionId = res.data.session_id
- expireAt = res.data.expire_at
- localStorage.setItem('session_id', sessionId)
- localStorage.setItem('expire_at', expireAt)
- console.log('new sessionId', sessionId)
- })
- }
- }
-
- fetchSession()
- }, [])
-
return (
// Handles Routing for an application
<HashRouter>
diff --git a/blocks/eda-frontend/src/utils/Api.js b/blocks/eda-frontend/src/utils/Api.js
index 96a11c51..307565e9 100644
--- a/blocks/eda-frontend/src/utils/Api.js
+++ b/blocks/eda-frontend/src/utils/Api.js
@@ -35,8 +35,40 @@ const api = axios.create({
withCredentials: true
})
-api.interceptors.request.use((config) => {
- const sessionId = localStorage.getItem('session_id')
+const isSessionExpired = () => {
+ const expireAt = localStorage.getItem('expire_at')
+ return !expireAt || new Date(expireAt) < new Date()
+}
+
+const deleteCookie = (name) => {
+ document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'
+}
+
+const refreshSession = async () => {
+ try {
+ const response = await api.get(`simulation/get_session?app_name=${process.env.REACT_APP_NAME}`)
+ const sessionId = response.data.session_id
+ const expireAt = response.data.expire_at
+ localStorage.setItem('session_id', sessionId)
+ localStorage.setItem('expire_at', expireAt)
+ console.log('Refreshed sessionId', sessionId)
+ return sessionId
+ } catch (error) {
+ console.error('Failed to refresh session', error)
+ return null
+ }
+}
+
+api.interceptors.request.use(async (config) => {
+ let sessionId = localStorage.getItem('session_id')
+
+ // Check if session is expired and refresh if necessary
+ if (!sessionId || isSessionExpired()) {
+ console.log('Session expired, refreshing...')
+ deleteCookie('sessionid')
+ sessionId = await refreshSession()
+ }
+
if (sessionId) {
config.headers['Session-ID'] = sessionId // Custom header for session ID
console.log('sessionId', sessionId)