fixed resolution
This commit is contained in:
parent
8325ad25bc
commit
43279bc5b7
@ -40,12 +40,18 @@ const RequireRole = ({ allowedRoles = [], children }) => {
|
||||
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 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 <Navigate to="/index" state={{ from: location.pathname }} replace />;
|
||||
}
|
||||
|
||||
// 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 <Navigate to="/index" replace />;
|
||||
}
|
||||
|
||||
// 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 <Navigate to="/index" replace />;
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
// Default redirect if no conditions are met
|
||||
return <Navigate to="/index" replace />;
|
||||
return children;
|
||||
};
|
||||
|
||||
const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
|
||||
@ -248,44 +249,35 @@ function App() {
|
||||
<Route
|
||||
path="/admin/dashboard"
|
||||
element={
|
||||
<RequireRole allowedRoles={['Admin']}>
|
||||
<ProtectedRoute allowedRoles={['Admin']}>
|
||||
<AdminDashboard />
|
||||
</RequireRole>
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/admin/validations"
|
||||
element={
|
||||
<RequireRole allowedRoles={['Admin']}>
|
||||
<ProtectedRoute allowedRoles={['Admin']}>
|
||||
<Validations />
|
||||
</RequireRole>
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/admin/validations/:id"
|
||||
element={
|
||||
<RequireRole allowedRoles={['Admin']}>
|
||||
<ProtectedRoute allowedRoles={['Admin']}>
|
||||
<ValidationReview />
|
||||
</RequireRole>
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
{/* <Route path="/admin/configuration">
|
||||
<Route
|
||||
index
|
||||
element={
|
||||
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
|
||||
<Configuration />
|
||||
</RequireRole>
|
||||
}
|
||||
/>
|
||||
</Route> */}
|
||||
<Route path="/admin/configuration/*" element={
|
||||
<RequireRole allowedRoles={['Admin', 'EstablishmentUser']}>
|
||||
<Route
|
||||
path="/admin/configuration/*"
|
||||
element={
|
||||
<ProtectedRoute allowedRoles={['Admin', 'EstablishmentUser']}>
|
||||
<Configuration />
|
||||
</RequireRole>
|
||||
} />
|
||||
{/* /> */}
|
||||
{/* </Route> */}
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/manufacturing-index"
|
||||
element={
|
||||
|
||||
Loading…
Reference in New Issue
Block a user