This commit is contained in:
Malini 2026-01-23 14:22:28 +05:30
parent 65fdeb349e
commit 804e84a74b

View File

@ -19,27 +19,27 @@ import PublicContactForm from '@/pages/PublicContactForm';
import EditCompanyProfile from '@/pages/Admin/configuration/EditCompanyProfile'; import EditCompanyProfile from '@/pages/Admin/configuration/EditCompanyProfile';
import Navbar from './pages/LandingPage/component/Navbar'; import Navbar from './pages/LandingPage/component/Navbar';
import ManufacturingIndex from '@/pages/ManufacturingIndex/ManufacturingIndex'; import ManufacturingIndex from '@/pages/ManufacturingIndex/ManufacturingIndex';
const RequireOTPVerification = ({ children }) => { const RequireOTPVerification = ({ children }) => {
const [isVerified, setIsVerified] = useState(false); const [isVerified, setIsVerified] = useState(false);
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
useEffect(() => { useEffect(() => {
const checkVerification = async () => { const checkVerification = async () => {
try { try {
const userProfile = JSON.parse(localStorage.getItem('user_profile') || '{}'); const userProfile = JSON.parse(localStorage.getItem('user_profile') || '{}');
// Check if user is logged in and has a valid role // Check if user is logged in and has a valid role
const role = localStorage.getItem('user_role'); const role = localStorage.getItem('user_role');
const allowedRoles = ['EstablishmentUser', 'Admin']; const allowedRoles = ['EstablishmentUser', 'Admin'];
if (!role || !allowedRoles.includes(role)) { if (!role || !allowedRoles.includes(role)) {
navigate('/index', { replace: true }); navigate('/index', { replace: true });
return; return;
} }
// Check if user is OTP verified // Check if user is OTP verified
if (userProfile.isOtpVerified) { if (userProfile.isOtpVerified) {
setIsVerified(true); setIsVerified(true);
@ -54,48 +54,48 @@ const RequireOTPVerification = ({ children }) => {
setIsLoading(false); setIsLoading(false);
} }
}; };
checkVerification(); checkVerification();
}, [navigate]); }, [navigate]);
if (isLoading) { if (isLoading) {
return <div className="flex items-center justify-center min-h-screen"> return <div className="flex items-center justify-center min-h-screen">
<div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary"></div> <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary"></div>
</div>; </div>;
} }
return isVerified ? children : null; return isVerified ? children : null;
}; };
const RequireRole = ({ allowedRoles = [], children }) => { const RequireRole = ({ allowedRoles = [], children }) => {
let role; let role;
try { try {
role = localStorage.getItem("user_role"); role = localStorage.getItem("user_role");
} catch { } catch {
role = null; role = null;
} }
const normalizedRole = String(role || "").toLowerCase(); const normalizedRole = String(role || "").toLowerCase();
const isAllowed = allowedRoles.some( const isAllowed = allowedRoles.some(
(allowed) => String(allowed || "").toLowerCase() === normalizedRole (allowed) => String(allowed || "").toLowerCase() === normalizedRole
); );
if (isAllowed) { if (isAllowed) {
return children; return children;
} }
return <Navigate to="/index" replace />; return <Navigate to="/index" replace />;
}; };
const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => { const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
if (!show) return null; if (!show) return null;
const minutes = Math.floor(remainingTime / 60); const minutes = Math.floor(remainingTime / 60);
const seconds = remainingTime % 60; const seconds = remainingTime % 60;
return ( return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm"> <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
<div className="bg-white rounded-lg shadow-xl max-w-md w-full mx-4 p-6"> <div className="bg-white rounded-lg shadow-xl max-w-md w-full mx-4 p-6">
@ -135,19 +135,19 @@ const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
</div> </div>
); );
}; };
function App() { function App() {
const [showWarning, setShowWarning] = React.useState(false); const [showWarning, setShowWarning] = React.useState(false);
const [warningTime, setWarningTime] = React.useState(0); const [warningTime, setWarningTime] = React.useState(0);
React.useEffect(() => { React.useEffect(() => {
const TIMEOUT_MINUTES = 15; const TIMEOUT_MINUTES = 15;
const WARNING_MINUTES = 2; const WARNING_MINUTES = 2;
let timer; let timer;
let warningTimer; let warningTimer;
let warningInterval; let warningInterval;
const clearAllTimers = () => { const clearAllTimers = () => {
clearTimeout(timer); clearTimeout(timer);
clearTimeout(warningTimer); clearTimeout(warningTimer);
@ -156,7 +156,7 @@ function App() {
warningTimer = null; warningTimer = null;
warningInterval = null; warningInterval = null;
}; };
const performLogout = () => { const performLogout = () => {
sessionStorage.clear(); sessionStorage.clear();
localStorage.removeItem('failed_attempts'); localStorage.removeItem('failed_attempts');
@ -165,56 +165,56 @@ function App() {
alert('Session expired. Please log in again.'); alert('Session expired. Please log in again.');
window.location.href = '/login'; window.location.href = '/login';
}; };
const showWarningModal = () => { const showWarningModal = () => {
setShowWarning(true); setShowWarning(true);
let timeLeft = WARNING_MINUTES * 60; let timeLeft = WARNING_MINUTES * 60;
setWarningTime(timeLeft); setWarningTime(timeLeft);
warningInterval = setInterval(() => { warningInterval = setInterval(() => {
timeLeft--; timeLeft--;
setWarningTime(timeLeft); setWarningTime(timeLeft);
if (timeLeft <= 0) { if (timeLeft <= 0) {
clearInterval(warningInterval); clearInterval(warningInterval);
performLogout(); performLogout();
} }
}, 1000); }, 1000);
}; };
const resetTimer = () => { const resetTimer = () => {
clearAllTimers(); clearAllTimers();
setShowWarning(false); setShowWarning(false);
warningTimer = setTimeout(() => { warningTimer = setTimeout(() => {
showWarningModal(); showWarningModal();
}, (TIMEOUT_MINUTES - WARNING_MINUTES) * 60 * 1000); }, (TIMEOUT_MINUTES - WARNING_MINUTES) * 60 * 1000);
timer = setTimeout(() => { timer = setTimeout(() => {
performLogout(); performLogout();
}, TIMEOUT_MINUTES * 60 * 1000); }, TIMEOUT_MINUTES * 60 * 1000);
}; };
const handleExtendSession = () => { const handleExtendSession = () => {
setShowWarning(false); setShowWarning(false);
resetTimer(); resetTimer();
}; };
const handleLogoutNow = () => { const handleLogoutNow = () => {
clearAllTimers(); clearAllTimers();
performLogout(); performLogout();
}; };
window.handleExtendSession = handleExtendSession; window.handleExtendSession = handleExtendSession;
window.handleLogoutNow = handleLogoutNow; window.handleLogoutNow = handleLogoutNow;
const events = ['mousemove', 'keydown', 'click', 'scroll', 'touchstart']; const events = ['mousemove', 'keydown', 'click', 'scroll', 'touchstart'];
events.forEach(event => { events.forEach(event => {
window.addEventListener(event, resetTimer); window.addEventListener(event, resetTimer);
}); });
resetTimer(); resetTimer();
return () => { return () => {
clearAllTimers(); clearAllTimers();
events.forEach(event => { events.forEach(event => {
@ -224,16 +224,16 @@ function App() {
delete window.handleLogoutNow; delete window.handleLogoutNow;
}; };
}, []); }, []);
return ( return (
<> <>
<SessionWarningModal <SessionWarningModal
show={showWarning} show={showWarning}
remainingTime={warningTime} remainingTime={warningTime}
onExtend={() => window.handleExtendSession?.()} onExtend={() => window.handleExtendSession?.()}
onLogout={() => window.handleLogoutNow?.()} onLogout={() => window.handleLogoutNow?.()}
/> />
<Routes> <Routes>
<Route path="/login" element={<Login />} /> <Route path="/login" element={<Login />} />
<Route <Route
@ -302,23 +302,28 @@ function App() {
} }
/> />
</Route> */} </Route> */}
<Route path="/admin/configuration/*" element={ <Route
<RequireOTPVerification> path="/admin/configuration/*"
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}> element={
<Configuration /> <RequireOTPVerification>
</RequireRole> <RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
</RequireOTPVerification> <Configuration />
} /> </RequireRole>
</RequireOTPVerification>
}
/>
{/* /> */} {/* /> */}
{/* </Route> */} {/* </Route> */}
<Route <Route
path="/manufacturing-index" path="/manufacturing-index"
element={ element={
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}> <RequireOTPVerification>
<ManufacturingIndex /> <RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
</RequireRole> <ManufacturingIndex />
} </RequireRole>
/> </RequireOTPVerification>
}
/>
<Route <Route
path="/admin/users" path="/admin/users"
element={ element={
@ -365,13 +370,13 @@ function App() {
<EditCompanyProfile /> <EditCompanyProfile />
</RequireRole> </RequireRole>
} /> */} } /> */}
{/* <Route {/* <Route
path="/admin/configuration/profile/:id?" path="/admin/configuration/profile/:id?"
element={ element={
<RequireRole allowedRoles={['EstablishmentUser']}> <RequireRole allowedRoles={['EstablishmentUser']}>
<EditCompanyProfile /> <EditCompanyProfile />
</RequireRole> </RequireRole>
} }
/> */} /> */}
<Route <Route
path="/edit-profile/:id" path="/edit-profile/:id"
@ -388,11 +393,11 @@ function App() {
<Route path="/reset-password" element={<PublicContactForm />} /> <Route path="/reset-password" element={<PublicContactForm />} />
<Route path="*" element={<Navigate to="/" replace />} /> <Route path="*" element={<Navigate to="/" replace />} />
{/* landingpage */} {/* landingpage */}
<Route path="/index" element={<Navbar />} /> <Route path="/index" element={<Navbar />} />
</Routes> </Routes>
</> </>
); );
} }
export default App; export default App;