fixed resolution
This commit is contained in:
parent
92d1628a8a
commit
8325ad25bc
@ -1,5 +1,5 @@
|
|||||||
import React from 'react';
|
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 Dashboard from '@/pages/Dashboard/Dashboard';
|
||||||
import AdminDashboard from '@/pages/Admin/AdminDashboard';
|
import AdminDashboard from '@/pages/Admin/AdminDashboard';
|
||||||
import AdminUsers from '@/pages/Admin/AdminUsers';
|
import AdminUsers from '@/pages/Admin/AdminUsers';
|
||||||
@ -40,6 +40,40 @@ const RequireRole = ({ allowedRoles = [], children }) => {
|
|||||||
return <Navigate to="/index" replace />;
|
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 }) => {
|
const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => {
|
||||||
if (!show) return null;
|
if (!show) return null;
|
||||||
@ -198,17 +232,17 @@ function App() {
|
|||||||
<Route
|
<Route
|
||||||
path="/history"
|
path="/history"
|
||||||
element={
|
element={
|
||||||
<RequireRole allowedRoles={['EstablishmentUser']}>
|
<ProtectedRoute allowedRoles={['EstablishmentUser']}>
|
||||||
<History />
|
<History />
|
||||||
</RequireRole>
|
</ProtectedRoute>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
path="/dashboard"
|
path="/dashboard"
|
||||||
element={
|
element={
|
||||||
<RequireRole allowedRoles={['EstablishmentUser']}>
|
<ProtectedRoute allowedRoles={['EstablishmentUser', 'Admin']}>
|
||||||
<Dashboard />
|
<Dashboard />
|
||||||
</RequireRole>
|
</ProtectedRoute>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
|
|||||||
@ -103,9 +103,6 @@ const OTPVerification = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (response?.status === 'success') {
|
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
|
// Check for admin role in multiple possible locations
|
||||||
const userRole = localStorage.getItem('user_role') ||
|
const userRole = localStorage.getItem('user_role') ||
|
||||||
response?.data?.user?.role ||
|
response?.data?.user?.role ||
|
||||||
@ -115,11 +112,25 @@ const OTPVerification = () => {
|
|||||||
const isAdmin = userRole?.toLowerCase() === 'admin' ||
|
const isAdmin = userRole?.toLowerCase() === 'admin' ||
|
||||||
response?.data?.isAdmin === true;
|
response?.data?.isAdmin === true;
|
||||||
|
|
||||||
// Use the redirect path from location state if available, otherwise determine based on role
|
// For non-admin users, set OTP verification flag
|
||||||
const redirectPath = location.state?.from ||
|
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');
|
(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);
|
setLoading(true);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
navigate(redirectPath, { replace: true });
|
navigate(redirectPath, { replace: true });
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user