|
1 | | -// auth.ts |
2 | | -import { signInWithRedirect, GoogleAuthProvider, User, UserCredential, getRedirectResult, signOut } from "firebase/auth"; |
| 1 | +import { |
| 2 | + signInWithRedirect, GoogleAuthProvider, User, UserCredential, |
| 3 | + getRedirectResult, signOut |
| 4 | +} from "firebase/auth"; |
3 | 5 | import { doc, getDoc } from "firebase/firestore"; |
4 | 6 | import { auth, firestore } from "./config"; |
5 | | -import { NavigateFunction } from "react-router-dom"; |
6 | 7 |
|
7 | | -// 🔹 Track Authentication State Changes |
8 | | -export function onAuthStateChanged(callback: (authUser: User | null) => void) { |
9 | | - return auth.onAuthStateChanged(callback); |
10 | | -} |
11 | | - |
12 | | -// 🔹 Google Sign-In with Redirect |
| 8 | +// 🔹 Google Sign-In (Redirect) |
13 | 9 | export async function signInWithGoogle(): Promise<void> { |
14 | 10 | const provider = new GoogleAuthProvider(); |
15 | 11 | try { |
16 | | - await signInWithRedirect(auth, provider); |
| 12 | + await signInWithRedirect(auth, provider); |
17 | 13 | } catch (error) { |
18 | | - console.error("Error signing in with Google:", error); |
19 | | - throw error; |
| 14 | + console.error("Error signing in with Google:", error); |
| 15 | + throw error; |
20 | 16 | } |
21 | 17 | } |
22 | 18 |
|
23 | | -// 🔹 Handle Redirected Sign-In Result (Now Redirects to Home) |
24 | | -export async function handleRedirectResult(navigate: NavigateFunction): Promise<{ isAdmin: boolean } | null> { |
25 | | - console.log("Handling redirect result..."); |
26 | | - |
| 19 | +// 🔹 Handle Redirected Sign-In & Check Admin Status |
| 20 | +export async function handleRedirectResult(): Promise<{ isAdmin: boolean } | null> { |
27 | 21 | try { |
28 | | - const result: UserCredential | null = await getRedirectResult(auth); |
29 | | - console.log("Redirect result:", result); |
30 | | - |
31 | | - if (!result || !result.user) return null; |
| 22 | + const result: UserCredential | null = await getRedirectResult(auth); |
| 23 | + if (!result || !result.user) return null; |
32 | 24 |
|
33 | | - const user: User = result.user; |
34 | | - if (!user.email) throw new Error("Google sign-in failed"); |
| 25 | + const user: User = result.user; |
| 26 | + if (!user.email) throw new Error("Google sign-in failed"); |
35 | 27 |
|
36 | | - console.log("User logged in:", user.email); |
| 28 | + console.log("User logged in:", user.email); |
37 | 29 |
|
38 | | - // 🔹 Restrict login to "gecskp.ac.in" emails, except for a specific admin email |
39 | | - const allowedEmailPattern = /^[a-zA-Z0-9]+@gecskp\.ac\.in$/; |
40 | | - const adminOverrideEmail = "codecompass2024@gmail.com"; |
| 30 | + // 🔹 Restrict login to "gecskp.ac.in" emails, except for admin |
| 31 | + const allowedEmailPattern = /^[a-zA-Z0-9]+@gecskp\.ac\.in$/; |
| 32 | + const adminOverrideEmail = "codecompass2024@gmail.com"; |
41 | 33 |
|
42 | | - if (user.email !== adminOverrideEmail && !allowedEmailPattern.test(user.email)) { |
43 | | - console.warn("Unauthorized email:", user.email); |
44 | | - throw new Error("Only GEC SKP emails are allowed"); |
45 | | - } |
| 34 | + if (user.email !== adminOverrideEmail && !allowedEmailPattern.test(user.email)) { |
| 35 | + console.warn("Unauthorized email:", user.email); |
| 36 | + throw new Error("Only GEC SKP emails are allowed"); |
| 37 | + } |
46 | 38 |
|
47 | | - // 🔹 Check if the user is an admin in Firestore |
48 | | - const userDocRef = doc(firestore, "adminemail", user.email); |
49 | | - const userDoc = await getDoc(userDocRef); |
50 | | - const isAdmin = userDoc.exists() && userDoc.data()?.role === "admin"; |
| 39 | + // 🔹 Check Firestore for Admin Role |
| 40 | + const userDocRef = doc(firestore, "adminemail", user.email); |
| 41 | + const userDoc = await getDoc(userDocRef); |
| 42 | + const isAdmin = userDoc.exists() && userDoc.data()?.role === "admin"; |
51 | 43 |
|
52 | | - // ✅ Redirect to home page after successful login |
53 | | - navigate("/"); |
| 44 | + // ✅ Store Admin Status in Local Storage |
| 45 | + localStorage.setItem("isAdmin", isAdmin.toString()); |
54 | 46 |
|
55 | | - return { isAdmin }; |
| 47 | + return { isAdmin }; |
56 | 48 | } catch (error) { |
57 | | - console.error("Error handling Google sign-in redirect result:", error); |
58 | | - return null; |
| 49 | + console.error("Error handling Google sign-in redirect result:", error); |
| 50 | + return null; |
59 | 51 | } |
60 | 52 | } |
61 | 53 |
|
62 | | -// 🔹 Sign Out Function |
| 54 | +// 🔹 Sign Out |
63 | 55 | export async function signOutWithGoogle(): Promise<void> { |
64 | 56 | try { |
65 | | - await signOut(auth); |
| 57 | + await signOut(auth); |
| 58 | + localStorage.removeItem("isAdmin"); // Clear admin state |
66 | 59 | } catch (error) { |
67 | | - console.error("Error signing out with Google:", error); |
68 | | - throw error; |
| 60 | + console.error("Error signing out:", error); |
| 61 | + throw error; |
69 | 62 | } |
70 | 63 | } |
0 commit comments