Skip to content

Commit 1f42a85

Browse files
File changes
1 parent 8f26390 commit 1f42a85

File tree

6 files changed

+1233
-39
lines changed

6 files changed

+1233
-39
lines changed

src/components/common/ProtectedRoute.jsx

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { useUserData } from '../hooks/useUserData.jsx';
1+
import { useEffect, useState } from 'react';
2+
import { base44 } from '@/api/base44Client';
23
import LoadingSpinner from './LoadingSpinner';
34

45
/**
5-
* ProtectedRoute wrapper for role-based access control
6+
* ProtectedRoute wrapper with auth check and returnTo support
67
*
78
* @param {boolean} requireAdmin - Only admins can access
89
* @param {boolean} requireFacilitator - Facilitators and admins can access
@@ -14,15 +15,48 @@ export default function ProtectedRoute({
1415
requireFacilitator = false,
1516
requireParticipant = false
1617
}) {
17-
const { user, loading } = useUserData(
18-
true,
19-
requireAdmin,
20-
requireFacilitator,
21-
requireParticipant
22-
);
18+
const [user, setUser] = useState(null);
19+
const [loading, setLoading] = useState(true);
2320

24-
if (loading || !user) {
25-
return <LoadingSpinner className="min-h-[60vh]" message="Loading..." />;
21+
useEffect(() => {
22+
const checkAuth = async () => {
23+
try {
24+
const currentUser = await base44.auth.me();
25+
26+
// Check role requirements
27+
if (requireAdmin && currentUser.role !== 'admin') {
28+
window.location.href = '/Dashboard';
29+
return;
30+
}
31+
32+
if (requireFacilitator && !['admin', 'facilitator'].includes(currentUser.role) && currentUser.user_type !== 'facilitator') {
33+
window.location.href = '/ParticipantPortal';
34+
return;
35+
}
36+
37+
if (requireParticipant && (currentUser.role === 'admin' || currentUser.user_type === 'facilitator')) {
38+
window.location.href = '/Dashboard';
39+
return;
40+
}
41+
42+
setUser(currentUser);
43+
setLoading(false);
44+
} catch (error) {
45+
// Not authenticated - redirect to login with return URL
46+
const returnTo = encodeURIComponent(window.location.pathname + window.location.search);
47+
base44.auth.redirectToLogin(`?returnTo=${returnTo}`);
48+
}
49+
};
50+
51+
checkAuth();
52+
}, [requireAdmin, requireFacilitator, requireParticipant]);
53+
54+
if (loading) {
55+
return <LoadingSpinner className="min-h-[60vh]" message="Authenticating..." />;
56+
}
57+
58+
if (!user) {
59+
return null;
2660
}
2761

2862
return <>{children}</>;

0 commit comments

Comments
 (0)