parent
ecc994299c
commit
e1f863a644
@ -1,5 +1,5 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Routes, Route, Navigate, useLocation } from 'react-router-dom';
|
import { Routes, Route, Navigate } 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,40 +40,6 @@ 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;
|
||||||
@ -232,17 +198,17 @@ function App() {
|
|||||||
<Route
|
<Route
|
||||||
path="/history"
|
path="/history"
|
||||||
element={
|
element={
|
||||||
<ProtectedRoute allowedRoles={['EstablishmentUser']}>
|
<RequireRole allowedRoles={['EstablishmentUser']}>
|
||||||
<History />
|
<History />
|
||||||
</ProtectedRoute>
|
</RequireRole>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
path="/dashboard"
|
path="/dashboard"
|
||||||
element={
|
element={
|
||||||
<ProtectedRoute allowedRoles={['EstablishmentUser', 'Admin']}>
|
<RequireRole allowedRoles={['EstablishmentUser']}>
|
||||||
<Dashboard />
|
<Dashboard />
|
||||||
</ProtectedRoute>
|
</RequireRole>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
|
|||||||
@ -103,6 +103,9 @@ 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 ||
|
||||||
@ -112,25 +115,11 @@ const OTPVerification = () => {
|
|||||||
const isAdmin = userRole?.toLowerCase() === 'admin' ||
|
const isAdmin = userRole?.toLowerCase() === 'admin' ||
|
||||||
response?.data?.isAdmin === true;
|
response?.data?.isAdmin === true;
|
||||||
|
|
||||||
// For non-admin users, set OTP verification flag
|
// Use the redirect path from location state if available, otherwise determine based on role
|
||||||
if (!isAdmin) {
|
const redirectPath = location.state?.from ||
|
||||||
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');
|
||||||
|
|
||||||
// Clean up the redirectAfterOTP from localStorage
|
// Wait for 5 seconds before redirecting
|
||||||
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