1- import { signInWithPopup , GoogleAuthProvider } from "firebase/auth" ;
1+ import { signInWithPopup , GoogleAuthProvider , onAuthStateChanged } from "firebase/auth" ;
22import { auth , db } from "../firebase" ;
3- import { doc , getDoc } from "firebase/firestore" ;
3+ import { doc , getDoc } from "firebase/firestore" ;
44import { useNavigate } from "react-router-dom" ;
55import { useEffect , useState } from "react" ;
66import type { User } from "firebase/auth" ;
77import DashboardLayout from "../component/DashboardLayout" ;
8- import { getAuth , onAuthStateChanged } from "firebase/auth " ;
8+ import { Spinner } from "react-bootstrap " ;
99import "../component/Home.css" ;
1010
1111
1212
1313const Home = ( ) => {
1414 const navigate = useNavigate ( ) ;
1515 const [ user , setUser ] = useState < User | null > ( null ) ;
16+ const [ checking , setChecking ] = useState ( true ) ;
1617
1718 useEffect ( ( ) => {
18- const unsubscribe = onAuthStateChanged ( getAuth ( ) , ( u ) => setUser ( u ) ) ;
19+ // Subscribe to auth state and validate user role from Firestore.
20+ const unsubscribe = onAuthStateChanged ( auth , async ( u ) => {
21+ setUser ( u ) ;
22+ if ( u ) {
23+ try {
24+ const adminRef = doc ( db , "users" , u . uid ) ;
25+ const adminSnap = await getDoc ( adminRef ) ;
26+ if ( ! adminSnap . exists ( ) ) {
27+ // Not an admin user in our DB — sign out and block access
28+ await auth . signOut ( ) ;
29+ setUser ( null ) ;
30+ // don't navigate here; let UI show sign-in after checking
31+ alert ( "You are not authorized to access this admin panel." ) ;
32+ } else {
33+ const userData = adminSnap . data ( ) ;
34+ if ( userData && userData . role === "user" ) {
35+ await auth . signOut ( ) ;
36+ setUser ( null ) ;
37+ alert ( "You are not authorized to access this admin panel." ) ;
38+ }
39+ }
40+ } catch ( err ) {
41+ console . error ( "Error validating user:" , err ) ;
42+ }
43+ }
44+ setChecking ( false ) ;
45+ } ) ;
1946 return ( ) => unsubscribe ( ) ;
2047 } , [ ] ) ;
2148
@@ -43,7 +70,7 @@ const Home = () => {
4370 const userData = adminSnap . data ( ) ;
4471 const validRoles = [ "admin" , "event-admin" , "event-coordinator" , "outreach-admin" , "outreach-member" , "finance" ] ;
4572
46- if ( ! validRoles . includes ( userData . role ) || userData . disabled ) {
73+ if ( ! validRoles . includes ( userData . role ) ) {
4774 await auth . signOut ( ) ;
4875 alert ( "You are not authorized to access this admin panel." ) ;
4976 return ;
@@ -56,6 +83,17 @@ const Home = () => {
5683 }
5784 } ;
5885
86+ if ( checking ) {
87+ return (
88+ < div className = "home-container" >
89+ < div className = "login-card text-center" >
90+ < Spinner animation = "border" />
91+ < p className = "mt-2" > Checking authentication...</ p >
92+ </ div >
93+ </ div >
94+ ) ;
95+ }
96+
5997 if ( ! user ) {
6098 return (
6199 < div className = "home-container" >
0 commit comments