mirror of
https://github.com/tinyhumansai/openhuman.git
synced 2026-07-30 06:32:24 +00:00
- 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.
37 lines
964 B
TypeScript
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;
|