From fc4535943ce4a159813aaffc57cbb0fc179d4047 Mon Sep 17 00:00:00 2001 From: Malini Date: Wed, 7 Jan 2026 19:43:37 +0530 Subject: [PATCH] fixed --- ipi-survey-platform/src/App.jsx | 125 ++++++++++++++---- .../pages/OTPVerification/OTPVerification.jsx | 45 +++++-- 2 files changed, 131 insertions(+), 39 deletions(-) diff --git a/ipi-survey-platform/src/App.jsx b/ipi-survey-platform/src/App.jsx index a95194b..ab5f566 100644 --- a/ipi-survey-platform/src/App.jsx +++ b/ipi-survey-platform/src/App.jsx @@ -1,4 +1,6 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; +import { useNavigate, useLocation } from 'react-router-dom'; +import { requestOtp } from '@/services/auth/authService'; import { Routes, Route, Navigate } from 'react-router-dom'; import Dashboard from '@/pages/Dashboard/Dashboard'; import AdminDashboard from '@/pages/Admin/AdminDashboard'; @@ -18,6 +20,53 @@ import EditCompanyProfile from '@/pages/Admin/configuration/EditCompanyProfile'; import Navbar from './pages/LandingPage/component/Navbar'; import ManufacturingIndex from '@/pages/ManufacturingIndex/ManufacturingIndex'; +const RequireOTPVerification = ({ children }) => { + const [isVerified, setIsVerified] = useState(false); + const [isLoading, setIsLoading] = useState(true); + const navigate = useNavigate(); + const location = useLocation(); + + useEffect(() => { + const checkVerification = async () => { + try { + const userProfile = JSON.parse(localStorage.getItem('user_profile') || '{}'); + + // Check if user is logged in and has a valid role + const role = localStorage.getItem('user_role'); + const allowedRoles = ['EstablishmentUser', 'Admin']; + + if (!role || !allowedRoles.includes(role)) { + navigate('/index', { replace: true }); + return; + } + + // Check if user is OTP verified + if (userProfile.isOtpVerified) { + setIsVerified(true); + } else { + // If not verified, redirect to index page + navigate('/index', { replace: true }); + } + } catch (error) { + console.error('Verification check failed:', error); + navigate('/index', { replace: true }); + } finally { + setIsLoading(false); + } + }; + + checkVerification(); + }, [navigate]); + + if (isLoading) { + return
+
+
; + } + + return isVerified ? children : null; +}; + const RequireRole = ({ allowedRoles = [], children }) => { let role; @@ -206,33 +255,41 @@ function App() { - - + + + + + } /> - - + + + + + } /> - - + + + + + } /> - - + + + + + } /> {/* @@ -246,10 +303,12 @@ function App() { /> */} - - - } /> + + + + + + } /> {/* /> */} {/* */} - - + + + + + } /> } /> - - + + + + + } /> - - + + + + + } /> {/* @@ -311,9 +376,11 @@ function App() { - - + + + + + } /> } /> diff --git a/ipi-survey-platform/src/pages/OTPVerification/OTPVerification.jsx b/ipi-survey-platform/src/pages/OTPVerification/OTPVerification.jsx index 6eff3b8..636938f 100644 --- a/ipi-survey-platform/src/pages/OTPVerification/OTPVerification.jsx +++ b/ipi-survey-platform/src/pages/OTPVerification/OTPVerification.jsx @@ -106,20 +106,45 @@ const OTPVerification = () => { // 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 || - response?.data?.role; + // Get the user role from the most reliable source + const userRole = response?.data?.user?.role || + response?.data?.role || + localStorage.getItem('user_role'); + + // Set the role in localStorage if not already set + if (userRole && !localStorage.getItem('user_role')) { + localStorage.setItem('user_role', userRole); + } + + // Update user profile in local storage with OTP verification status + try { + const userProfile = JSON.parse(localStorage.getItem('user_profile') || '{}'); + localStorage.setItem('user_profile', JSON.stringify({ + ...userProfile, + isOtpVerified: true, + email: location.state?.email || userProfile.email, + role: userRole || userProfile.role + })); + } catch (error) { + console.error('Failed to update user profile:', error); + } + + // Determine the appropriate dashboard based on user role + let redirectPath = '/dashboard'; // Check if user is admin (case-insensitive check) - const isAdmin = userRole?.toLowerCase() === 'admin' || - response?.data?.isAdmin === true; + if (userRole && userRole.toLowerCase() === 'admin') { + redirectPath = '/admin/dashboard'; + console.log('Admin user detected, redirecting to admin dashboard'); + } - // Use the redirect path from location state if available, otherwise determine based on role - const redirectPath = location.state?.from || - (isAdmin ? '/admin/dashboard' : '/dashboard'); + // If there's a saved path from before login, use that + const savedPath = location.state?.from?.pathname; + if (savedPath) { + redirectPath = savedPath; + } - // Wait for 5 seconds before redirecting + // Redirect after a short delay setLoading(true); setTimeout(() => { navigate(redirectPath, { replace: true });