Skip to content

Commit f3bdaac

Browse files
committed
feat: enhance authentication flow in Home component with role validation and loading spinner
1 parent 7b87199 commit f3bdaac

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/pages/Home.tsx

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
1-
import { signInWithPopup, GoogleAuthProvider } from "firebase/auth";
1+
import { signInWithPopup, GoogleAuthProvider, onAuthStateChanged } from "firebase/auth";
22
import { auth, db } from "../firebase";
3-
import { doc, getDoc} from "firebase/firestore";
3+
import { doc, getDoc } from "firebase/firestore";
44
import { useNavigate } from "react-router-dom";
55
import { useEffect, useState } from "react";
66
import type { User } from "firebase/auth";
77
import DashboardLayout from "../component/DashboardLayout";
8-
import { getAuth, onAuthStateChanged } from "firebase/auth";
8+
import { Spinner } from "react-bootstrap";
99
import "../component/Home.css";
1010

1111

1212

1313
const 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">

src/utils/backend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { UserModel } from '../models/UserModel';
33
import type { Event } from '../models/Event';
44
import type { TeamMember } from '../models/teamRegModels';
55

6-
const BACKEND_URL = 'https://api.saturnalia.in';
6+
const BACKEND_URL = import.meta.env.VITE_REACT_APP_BACKEND_URL;
77
const UPLOAD_PASSWORD = import.meta.env.VITE_UPLOAD_PASSWORD;
88
const EMAIL_PASSWORD = import.meta.env.VITE_EMAIL_PASSWORD;
99

0 commit comments

Comments
 (0)