diff --git a/ipi-survey-platform/src/App.jsx b/ipi-survey-platform/src/App.jsx index ab5f566..6431aba 100644 --- a/ipi-survey-platform/src/App.jsx +++ b/ipi-survey-platform/src/App.jsx @@ -19,27 +19,27 @@ import PublicContactForm from '@/pages/PublicContactForm'; import EditCompanyProfile from '@/pages/Admin/configuration/EditCompanyProfile'; import Navbar from './pages/LandingPage/component/Navbar'; import ManufacturingIndex from '@/pages/ManufacturingIndex/ManufacturingIndex'; - + const RequireOTPVerification = ({ children }) => { const [isVerified, setIsVerified] = useState(false); const [isLoading, setIsLoading] = useState(true); const navigate = useNavigate(); const location = useLocation(); - + useEffect(() => { const checkVerification = async () => { try { const userProfile = JSON.parse(localStorage.getItem('user_profile') || '{}'); - + // Check if user is logged in and has a valid role const role = localStorage.getItem('user_role'); const allowedRoles = ['EstablishmentUser', 'Admin']; - + if (!role || !allowedRoles.includes(role)) { navigate('/index', { replace: true }); return; } - + // Check if user is OTP verified if (userProfile.isOtpVerified) { setIsVerified(true); @@ -54,48 +54,48 @@ const RequireOTPVerification = ({ children }) => { setIsLoading(false); } }; - + checkVerification(); }, [navigate]); - + if (isLoading) { return
; } - + return isVerified ? children : null; }; - + const RequireRole = ({ allowedRoles = [], children }) => { let role; - + try { role = localStorage.getItem("user_role"); } catch { role = null; } - + const normalizedRole = String(role || "").toLowerCase(); - + const isAllowed = allowedRoles.some( (allowed) => String(allowed || "").toLowerCase() === normalizedRole ); - + if (isAllowed) { return children; } - + return ; }; - - + + const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => { if (!show) return null; - + const minutes = Math.floor(remainingTime / 60); const seconds = remainingTime % 60; - + return (
@@ -135,19 +135,19 @@ const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
); }; - + function App() { const [showWarning, setShowWarning] = React.useState(false); const [warningTime, setWarningTime] = React.useState(0); - + React.useEffect(() => { - const TIMEOUT_MINUTES = 15; - const WARNING_MINUTES = 2; - + const TIMEOUT_MINUTES = 15; + const WARNING_MINUTES = 2; + let timer; let warningTimer; let warningInterval; - + const clearAllTimers = () => { clearTimeout(timer); clearTimeout(warningTimer); @@ -156,7 +156,7 @@ function App() { warningTimer = null; warningInterval = null; }; - + const performLogout = () => { sessionStorage.clear(); localStorage.removeItem('failed_attempts'); @@ -165,56 +165,56 @@ function App() { alert('Session expired. Please log in again.'); window.location.href = '/login'; }; - + const showWarningModal = () => { setShowWarning(true); let timeLeft = WARNING_MINUTES * 60; setWarningTime(timeLeft); - + warningInterval = setInterval(() => { timeLeft--; setWarningTime(timeLeft); - + if (timeLeft <= 0) { clearInterval(warningInterval); performLogout(); } }, 1000); }; - + const resetTimer = () => { clearAllTimers(); setShowWarning(false); - + warningTimer = setTimeout(() => { showWarningModal(); }, (TIMEOUT_MINUTES - WARNING_MINUTES) * 60 * 1000); - + timer = setTimeout(() => { performLogout(); }, TIMEOUT_MINUTES * 60 * 1000); }; - + const handleExtendSession = () => { setShowWarning(false); resetTimer(); }; - + const handleLogoutNow = () => { clearAllTimers(); performLogout(); }; - + window.handleExtendSession = handleExtendSession; window.handleLogoutNow = handleLogoutNow; - + const events = ['mousemove', 'keydown', 'click', 'scroll', 'touchstart']; events.forEach(event => { window.addEventListener(event, resetTimer); }); - + resetTimer(); - + return () => { clearAllTimers(); events.forEach(event => { @@ -224,16 +224,16 @@ function App() { delete window.handleLogoutNow; }; }, []); - + return ( <> - window.handleExtendSession?.()} onLogout={() => window.handleLogoutNow?.()} /> - + } /> */} - - - - - - } /> + + + + + + } +/> {/* /> */} {/* */} - - - - } - /> + + + + + + } +/> } /> */} - {/* - } + } /> */} } /> } /> {/* landingpage */} - + } /> ); } - + export default App; \ No newline at end of file