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