1- // Import User and other necessary types/functions from Firebase Auth
2- import { signInWithPopup , GoogleAuthProvider , User , UserCredential } from 'firebase/auth' ;
1+ // Import necessary Firebase Auth functions
2+ import { signInWithRedirect , GoogleAuthProvider , User , UserCredential , getRedirectResult } from 'firebase/auth' ;
33import { doc , getDoc } from 'firebase/firestore' ;
44import { auth , firestore } from './config' ; // Ensure you are correctly importing from your Firebase config
55
@@ -13,38 +13,45 @@ export async function signInWithGoogle(): Promise<{ isAdmin: boolean }> {
1313 const provider = new GoogleAuthProvider ( ) ;
1414
1515 try {
16- const result : UserCredential = await signInWithPopup ( auth , provider ) ;
17- const user : User = result . user ;
16+ await signInWithRedirect ( auth , provider ) ; // ✅ Redirect-based authentication for iframes
17+ return { isAdmin : false } ; // Temporary return value, actual role check happens after redirect
18+ } catch ( error ) {
19+ console . error ( 'Error signing in with Google:' , error ) ;
20+ throw error ;
21+ }
22+ }
1823
19- if ( ! user || ! user . email ) {
20- throw new Error ( 'Google sign-in failed' ) ;
21- }
24+ // Handle the redirected sign-in result (should be called on page load)
25+ export async function handleRedirectResult ( ) : Promise < { isAdmin : boolean } | null > {
26+ try {
27+ const result : UserCredential | null = await getRedirectResult ( auth ) ;
2228
23- // Restrict login to only emails from "gecskp.ac.in"
24- // Restrict login to only emails from "gecskp.ac.in", except for a specific admin email
25- const allowedEmailPattern = / ^ [ a - z A - Z 0 - 9 ] + @ g e c s k p \. a c \. i n $ / ;
26- const adminOverrideEmail = "codecompass2024@gmail.com" ;
29+ if ( ! result || ! result . user ) return null ; // No redirect result
2730
28- if ( user . email !== adminOverrideEmail && ! allowedEmailPattern . test ( user . email ) ) {
29- throw new Error ( 'Only GEC SKP emails are allowed' ) ;
30- }
31+ const user : User = result . user ;
32+ if ( ! user . email ) throw new Error ( 'Google sign-in failed' ) ;
3133
32-
34+ // Restrict login to only emails from "gecskp.ac.in", except for a specific admin email
35+ const allowedEmailPattern = / ^ [ a - z A - Z 0 - 9 ] + @ g e c s k p \. a c \. i n $ / ;
36+ const adminOverrideEmail = "codecompass2024@gmail.com" ;
3337
38+ if ( user . email !== adminOverrideEmail && ! allowedEmailPattern . test ( user . email ) ) {
39+ throw new Error ( 'Only GEC SKP emails are allowed' ) ;
40+ }
41+
42+ // Check if user is an admin in Firestore
3443 const userDocRef = doc ( firestore , 'adminemail' , user . email ) ;
3544 const userDoc = await getDoc ( userDocRef ) ;
36-
3745 const isAdmin = userDoc . exists ( ) && userDoc . data ( ) ?. role === 'admin' ;
3846
3947 return { isAdmin } ;
4048 } catch ( error ) {
41- console . error ( 'Error signing in with Google :' , error ) ;
42- throw error ;
49+ console . error ( 'Error handling Google sign- in redirect result :' , error ) ;
50+ return null ;
4351 }
4452}
4553
46-
47-
54+ // Function to sign out
4855export async function signOutWithGoogle ( ) : Promise < void > {
4956 try {
5057 await auth . signOut ( ) ;
0 commit comments