fixed resolution

This commit is contained in:
Malini 2026-01-07 17:38:41 +05:30
parent 8325ad25bc
commit 43279bc5b7

View File

@ -40,12 +40,18 @@ const RequireRole = ({ allowedRoles = [], children }) => {
return <Navigate to="/index" replace />; return <Navigate to="/index" replace />;
}; };
// New ProtectedRoute component to check OTP verification // ProtectedRoute component to check authentication and OTP verification
const ProtectedRoute = ({ children, allowedRoles = [] }) => { const ProtectedRoute = ({ children, allowedRoles = [] }) => {
const location = useLocation(); const location = useLocation();
const token = localStorage.getItem('token');
const isOTPVerified = localStorage.getItem('isOTPVerified') === 'true'; const isOTPVerified = localStorage.getItem('isOTPVerified') === 'true';
const userRole = localStorage.getItem('user_role') || ''; const userRole = localStorage.getItem('user_role') || '';
// If no token, redirect to login
if (!token) {
return <Navigate to="/index" state={{ from: location.pathname }} replace />;
}
// Check if user has the required role // Check if user has the required role
const hasRequiredRole = allowedRoles.some(role => const hasRequiredRole = allowedRoles.some(role =>
role.toLowerCase() === userRole.toLowerCase() role.toLowerCase() === userRole.toLowerCase()
@ -56,23 +62,18 @@ const ProtectedRoute = ({ children, allowedRoles = [] }) => {
return <Navigate to="/index" replace />; return <Navigate to="/index" replace />;
} }
// For Admin users, no OTP check needed // For all users, check OTP verification
if (userRole.toLowerCase() === 'admin') { // Skip OTP check for the OTP verification page itself
return children; if (!location.pathname.includes('/verify-otp')) {
} // If OTP not verified, redirect to OTP verification
if (!isOTPVerified) {
// For Establishment users, check OTP verification
if (userRole.toLowerCase() === 'establishmentuser' || userRole.toLowerCase() === 'establishment') {
if (!isOTPVerified && !location.pathname.includes('/verify-otp')) {
// Store the intended URL for redirecting after OTP verification // Store the intended URL for redirecting after OTP verification
localStorage.setItem('redirectAfterOTP', location.pathname); localStorage.setItem('redirectAfterOTP', location.pathname);
return <Navigate to="/index" replace />; return <Navigate to="/index" replace />;
} }
return children;
} }
// Default redirect if no conditions are met return children;
return <Navigate to="/index" replace />;
}; };
const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => { const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
@ -248,44 +249,35 @@ function App() {
<Route <Route
path="/admin/dashboard" path="/admin/dashboard"
element={ element={
<RequireRole allowedRoles={['Admin']}> <ProtectedRoute allowedRoles={['Admin']}>
<AdminDashboard /> <AdminDashboard />
</RequireRole> </ProtectedRoute>
} }
/> />
<Route <Route
path="/admin/validations" path="/admin/validations"
element={ element={
<RequireRole allowedRoles={['Admin']}> <ProtectedRoute allowedRoles={['Admin']}>
<Validations /> <Validations />
</RequireRole> </ProtectedRoute>
} }
/> />
<Route <Route
path="/admin/validations/:id" path="/admin/validations/:id"
element={ element={
<RequireRole allowedRoles={['Admin']}> <ProtectedRoute allowedRoles={['Admin']}>
<ValidationReview /> <ValidationReview />
</RequireRole> </ProtectedRoute>
} }
/> />
{/* <Route path="/admin/configuration"> <Route
<Route path="/admin/configuration/*"
index element={
element={ <ProtectedRoute allowedRoles={['Admin', 'EstablishmentUser']}>
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
<Configuration />
</RequireRole>
}
/>
</Route> */}
<Route path="/admin/configuration/*" element={
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
<Configuration /> <Configuration />
</RequireRole> </ProtectedRoute>
} /> }
{/* /> */} />
{/* </Route> */}
<Route <Route
path="/manufacturing-index" path="/manufacturing-index"
element={ element={