Skip to content

Commit 991c6a5

Browse files
fix:submission page guard
1 parent a823e73 commit 991c6a5

1 file changed

Lines changed: 46 additions & 19 deletions

File tree

components/SubmissionGuard.tsx

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,65 @@
22

33
import React, { useEffect, useState } from "react";
44
import { useRouter } from "next/navigation";
5-
5+
import { useAuth } from "./AuthProvider";
66
interface SubmissionGuardProps {
77
children: React.ReactNode;
88
}
99

1010
export default function SubmissionGuard({ children }: SubmissionGuardProps) {
1111
const router = useRouter();
12-
const [loading, setLoading] = useState(true);
12+
const { user, loading, hasTeam, teamData } = useAuth();
1313

1414
useEffect(() => {
15-
// Add your team/submission verification logic here
16-
// Example: Check if the user is in a team or is a team leader
17-
const checkAccess = async () => {
18-
try {
19-
// Place your fetch/state check here
20-
setLoading(false);
21-
} catch (error) {
22-
console.error("Access verification failed:", error);
23-
router.push("/dashboard");
24-
}
25-
};
26-
27-
checkAccess();
28-
}, [router]);
15+
// Wait until AuthProvider completes loading user and team data
16+
if (loading) return;
17+
18+
// 1. Not logged in -> Redirect to Login
19+
if (!user) {
20+
router.replace("/login");
21+
return;
22+
}
23+
24+
// 2. Not in a team -> Redirect to Dashboard
25+
if (!hasTeam || !teamData) {
26+
router.replace("/dashboard");
27+
return;
28+
}
29+
30+
// 3. Not team leader -> Redirect to Dashboard
31+
// Fallback checks both API's boolean flag and leader identifier
32+
const isLeader =
33+
teamData.isLeader ??
34+
(teamData.leaderId === user.uid || teamData.leaderId === user.email);
35+
36+
if (!isLeader) {
37+
router.replace("/dashboard");
38+
}
39+
}, [user, loading, hasTeam, teamData, router]);
2940

41+
// Show a loading screen while auth/team status resolves
3042
if (loading) {
3143
return (
32-
<div className="flex items-center justify-center min-h-screen">
33-
<p>Loading submission page...</p>
44+
<div className="flex items-center justify-center min-h-screen bg-[#070e04] text-white">
45+
<p className="font-mono text-lg animate-pulse">
46+
Verifying submission permissions...
47+
</p>
3448
</div>
3549
);
3650
}
3751

52+
// Calculate authorized status
53+
const isAuthorized =
54+
user &&
55+
hasTeam &&
56+
teamData &&
57+
(teamData.isLeader ??
58+
(teamData.leaderId === user.uid || teamData.leaderId === user.email));
59+
60+
// Prevent flash of guarded content before redirect occurs
61+
if (!isAuthorized) {
62+
return null;
63+
}
64+
3865
return <>{children}</>;
39-
}
66+
}

0 commit comments

Comments
 (0)