fixed resolution

This commit is contained in:
Malini 2026-01-07 17:08:07 +05:30
parent 92d1628a8a
commit 8325ad25bc
2 changed files with 56 additions and 11 deletions

View File

@ -1,5 +1,5 @@
import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { Routes, Route, Navigate, useLocation } from 'react-router-dom';
import Dashboard from '@/pages/Dashboard/Dashboard';
import AdminDashboard from '@/pages/Admin/AdminDashboard';
import AdminUsers from '@/pages/Admin/AdminUsers';
@ -40,6 +40,40 @@ const RequireRole = ({ allowedRoles = [], children }) => {
return <Navigate to="/index" replace />;
};
// New ProtectedRoute component to check OTP verification
const ProtectedRoute = ({ children, allowedRoles = [] }) => {
const location = useLocation();
const isOTPVerified = localStorage.getItem('isOTPVerified') === 'true';
const userRole = localStorage.getItem('user_role') || '';
// Check if user has the required role
const hasRequiredRole = allowedRoles.some(role =>
role.toLowerCase() === userRole.toLowerCase()
);
// If user doesn't have required role, redirect to index
if (!hasRequiredRole) {
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')) {
// 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 />;
};
const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
if (!show) return null;
@ -198,17 +232,17 @@ function App() {
<Route
path="/history"
element={
<RequireRole allowedRoles={['EstablishmentUser']}>
<ProtectedRoute allowedRoles={['EstablishmentUser']}>
<History />
</RequireRole>
</ProtectedRoute>
}
/>
<Route
path="/dashboard"
element={
<RequireRole allowedRoles={['EstablishmentUser']}>
<ProtectedRoute allowedRoles={['EstablishmentUser', 'Admin']}>
<Dashboard />
</RequireRole>
</ProtectedRoute>
}
/>
<Route

View File

@ -103,9 +103,6 @@ const OTPVerification = () => {
});
if (response?.status === 'success') {
// Show success message immediately
showToast('success', 'Verified! You are signed in. Redirecting to your dashboard.');
// Check for admin role in multiple possible locations
const userRole = localStorage.getItem('user_role') ||
response?.data?.user?.role ||
@ -115,11 +112,25 @@ const OTPVerification = () => {
const isAdmin = userRole?.toLowerCase() === 'admin' ||
response?.data?.isAdmin === true;
// Use the redirect path from location state if available, otherwise determine based on role
const redirectPath = location.state?.from ||
// For non-admin users, set OTP verification flag
if (!isAdmin) {
localStorage.setItem('isOTPVerified', 'true');
}
// Show success message
showToast('success', 'Verified! You are signed in. Redirecting to your dashboard.');
// Get the intended redirect path or use default based on role
const redirectPath = localStorage.getItem('redirectAfterOTP') ||
location.state?.from ||
(isAdmin ? '/admin/dashboard' : '/dashboard');
// Wait for 5 seconds before redirecting
// Clean up the redirectAfterOTP from localStorage
if (localStorage.getItem('redirectAfterOTP')) {
localStorage.removeItem('redirectAfterOTP');
}
// Redirect after a short delay
setLoading(true);
setTimeout(() => {
navigate(redirectPath, { replace: true });