diff --git a/ipi-survey-platform/src/App.jsx b/ipi-survey-platform/src/App.jsx index 6c812f6..3189b72 100644 --- a/ipi-survey-platform/src/App.jsx +++ b/ipi-survey-platform/src/App.jsx @@ -40,12 +40,18 @@ const RequireRole = ({ allowedRoles = [], children }) => { return ; }; -// New ProtectedRoute component to check OTP verification +// ProtectedRoute component to check authentication and OTP verification const ProtectedRoute = ({ children, allowedRoles = [] }) => { const location = useLocation(); + const token = localStorage.getItem('token'); const isOTPVerified = localStorage.getItem('isOTPVerified') === 'true'; const userRole = localStorage.getItem('user_role') || ''; + // If no token, redirect to login + if (!token) { + return ; + } + // Check if user has the required role const hasRequiredRole = allowedRoles.some(role => role.toLowerCase() === userRole.toLowerCase() @@ -56,23 +62,18 @@ const ProtectedRoute = ({ children, allowedRoles = [] }) => { return ; } - // For Admin users, no OTP check needed - if (userRole.toLowerCase() === 'admin') { - return children; - } - - // For Establishment users, check OTP verification - if (userRole.toLowerCase() === 'establishmentuser' || userRole.toLowerCase() === 'establishment') { - if (!isOTPVerified && !location.pathname.includes('/verify-otp')) { + // For all users, check OTP verification + // Skip OTP check for the OTP verification page itself + if (!location.pathname.includes('/verify-otp')) { + // If OTP not verified, redirect to OTP verification + if (!isOTPVerified) { // Store the intended URL for redirecting after OTP verification localStorage.setItem('redirectAfterOTP', location.pathname); return ; } - return children; } - // Default redirect if no conditions are met - return ; + return children; }; const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => { @@ -248,44 +249,35 @@ function App() { + - + } /> + - + } /> + - + } /> - {/* - - - - } - /> - */} - + - - } /> - {/* /> */} - {/* */} + + } + />