Files
openhuman/src/components/ProtectedRoute.tsx
T
Steven Enamakel e05b1a2587 Enhance onboarding process with Redux state management
- Integrated Redux state management into the Onboarding component by dispatching the setOnboarded action upon completion.
- Updated authSlice to include isOnboarded state, allowing for tracking of onboarding status.
- Modified the Redux store configuration to persist the onboarding status alongside the token, improving user experience and state consistency.

These changes streamline the onboarding process and enhance state management for user authentication.
2026-01-28 04:33:28 +05:30

37 lines
964 B
TypeScript

import { Navigate } from 'react-router-dom';
import { useAppSelector } from '../store/hooks';
interface ProtectedRouteProps {
children: React.ReactNode;
requireAuth?: boolean;
requireOnboarded?: boolean;
redirectTo?: string;
}
/**
* Protected route component that handles authentication and onboarding checks
*/
const ProtectedRoute = ({
children,
requireAuth = true,
requireOnboarded = false,
redirectTo,
}: ProtectedRouteProps) => {
const token = useAppSelector((state) => state.auth.token);
const isOnboarded = useAppSelector((state) => state.auth.isOnboarded);
// If auth is required but user is not logged in
if (requireAuth && !token) {
return <Navigate to={redirectTo || '/'} replace />;
}
// If onboarding is required but user is not onboarded
if (requireOnboarded && !isOnboarded) {
return <Navigate to={redirectTo || '/onboarding'} replace />;
}
return <>{children}</>;
};
export default ProtectedRoute;