-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth.ts
More file actions
61 lines (50 loc) · 2.21 KB
/
auth.ts
File metadata and controls
61 lines (50 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Import User and other necessary types/functions from Firebase Auth
import { signInWithPopup, GoogleAuthProvider, User, UserCredential , getAuth } from 'firebase/auth';
import { doc, getDoc, enableNetwork, disableNetwork } from 'firebase/firestore';
import { auth, firestore } from './config'; // Ensure you are correctly importing from your Firebase config
// Function to track authentication state changes
export function onAuthStateChanged(callback: (authUser: User | null) => void) {
return auth.onAuthStateChanged(callback);
}
// Function for Google sign-in and role check
export async function signInWithGoogle(): Promise<{ user:User; isAdmin: boolean }> {
const provider = new GoogleAuthProvider();
provider.setCustomParameters({ display: "popup" }); // Force popup
try {
// Ensure network is enabled before attempting sign-in
await enableNetwork(firestore);
const result: UserCredential = await signInWithPopup(auth, provider);
const user: User = result.user;
if (!user || !user.email) {
throw new Error('Google sign-in failed');
}
// Restrict login to only emails from "gecskp.ac.in"
// Restrict login to only emails from "gecskp.ac.in", except for a specific admin email
const allowedEmailPattern = /^[a-zA-Z0-9]+@gecskp\.ac\.in$/;
const adminOverrideEmail = "codecompass2024@gmail.com";
if (user.email !== adminOverrideEmail && !allowedEmailPattern.test(user.email)) {
throw new Error('Only GEC SKP emails are allowed');
}
try {
const userDocRef = doc(firestore, 'adminemail', user.email);
const userDoc = await getDoc(userDocRef);
const isAdmin = userDoc.exists() && userDoc.data()?.role === 'admin';
return {user, isAdmin };
} catch (firestoreError) {
console.error('Error accessing Firestore:', firestoreError);
// If Firestore is offline, assume user is not admin for security
return {user, isAdmin: false};
}
} catch (error) {
console.error('Error signing in with Google:', error);
throw error;
}
}
export async function signOutWithGoogle(): Promise<void> {
try {
await auth.signOut();
} catch (error) {
console.error('Error signing out with Google:', error);
throw error;
}
}