parent
ecc994299c
commit
e1f863a644
@ -1,5 +1,5 @@
|
||||
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 AdminDashboard from '@/pages/Admin/AdminDashboard';
|
||||
import AdminUsers from '@/pages/Admin/AdminUsers';
|
||||
@ -40,40 +40,6 @@ 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;
|
||||
@ -232,17 +198,17 @@ function App() {
|
||||
<Route
|
||||
path="/history"
|
||||
element={
|
||||
<ProtectedRoute allowedRoles={['EstablishmentUser']}>
|
||||
<RequireRole allowedRoles={['EstablishmentUser']}>
|
||||
<History />
|
||||
</ProtectedRoute>
|
||||
</RequireRole>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/dashboard"
|
||||
element={
|
||||
<ProtectedRoute allowedRoles={['EstablishmentUser', 'Admin']}>
|
||||
<RequireRole allowedRoles={['EstablishmentUser']}>
|
||||
<Dashboard />
|
||||
</ProtectedRoute>
|
||||
</RequireRole>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
|
||||
@ -103,6 +103,9 @@ 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 ||
|
||||
@ -112,25 +115,11 @@ const OTPVerification = () => {
|
||||
const isAdmin = userRole?.toLowerCase() === 'admin' ||
|
||||
response?.data?.isAdmin === true;
|
||||
|
||||
// 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 ||
|
||||
// Use the redirect path from location state if available, otherwise determine based on role
|
||||
const redirectPath = location.state?.from ||
|
||||
(isAdmin ? '/admin/dashboard' : '/dashboard');
|
||||
|
||||
// Clean up the redirectAfterOTP from localStorage
|
||||
if (localStorage.getItem('redirectAfterOTP')) {
|
||||
localStorage.removeItem('redirectAfterOTP');
|
||||
}
|
||||
|
||||
// Redirect after a short delay
|
||||
// Wait for 5 seconds before redirecting
|
||||
setLoading(true);
|
||||
setTimeout(() => {
|
||||
navigate(redirectPath, { replace: true });
|
||||
|
||||
Loading…
Reference in New Issue
Block a user