From 8325ad25bc703c12bccc80862b5ca16f21ea7dbc Mon Sep 17 00:00:00 2001 From: Malini Date: Wed, 7 Jan 2026 17:08:07 +0530 Subject: [PATCH] fixed resolution --- ipi-survey-platform/src/App.jsx | 44 ++++++++++++++++--- .../pages/OTPVerification/OTPVerification.jsx | 23 +++++++--- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/ipi-survey-platform/src/App.jsx b/ipi-survey-platform/src/App.jsx index a95194b..6c812f6 100644 --- a/ipi-survey-platform/src/App.jsx +++ b/ipi-survey-platform/src/App.jsx @@ -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 ; }; +// 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 ; + } + + // 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 ; + } + return children; + } + + // Default redirect if no conditions are met + return ; +}; const SessionWarningModal = ({ show, remainingTime, onExtend, onLogout }) => { if (!show) return null; @@ -198,17 +232,17 @@ function App() { + - + } /> + - + } /> { }); 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 });