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 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 <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>;
}
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 <Navigate to="/index" replace />;
};
const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
if (!show) return null;
const minutes = Math.floor(remainingTime / 60);
const seconds = remainingTime % 60;
return (
<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">
@ -135,19 +135,19 @@ const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
</div>
);
};
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 (
<>
<SessionWarningModal
<SessionWarningModal
show={showWarning}
remainingTime={warningTime}
onExtend={() => window.handleExtendSession?.()}
onLogout={() => window.handleLogoutNow?.()}
/>
<Routes>
<Route path="/login" element={<Login />} />
<Route
@ -302,23 +302,28 @@ function App() {
}
/>
</Route> */}
<Route path="/admin/configuration/*" element={
<RequireOTPVerification>
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
<Configuration />
</RequireRole>
</RequireOTPVerification>
} />
<Route
path="/admin/configuration/*"
element={
<RequireOTPVerification>
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
<Configuration />
</RequireRole>
</RequireOTPVerification>
}
/>
{/* /> */}
{/* </Route> */}
<Route
path="/manufacturing-index"
element={
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
<ManufacturingIndex />
</RequireRole>
}
/>
<Route
path="/manufacturing-index"
element={
<RequireOTPVerification>
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
<ManufacturingIndex />
</RequireRole>
</RequireOTPVerification>
}
/>
<Route
path="/admin/users"
element={
@ -365,13 +370,13 @@ function App() {
<EditCompanyProfile />
</RequireRole>
} /> */}
{/* <Route
path="/admin/configuration/profile/:id?"
{/* <Route
path="/admin/configuration/profile/:id?"
element={
<RequireRole allowedRoles={['EstablishmentUser']}>
<EditCompanyProfile />
</RequireRole>
}
}
/> */}
<Route
path="/edit-profile/:id"
@ -388,11 +393,11 @@ function App() {
<Route path="/reset-password" element={<PublicContactForm />} />
<Route path="*" element={<Navigate to="/" replace />} />
{/* landingpage */}
<Route path="/index" element={<Navbar />} />
</Routes>
</>
);
}
export default App;