Skip to content

Commit 47188e1

Browse files
committed
after makeing signwithredirect it is logging in but not redirecting to home page so fixed that
1 parent 07b0ff4 commit 47188e1

1 file changed

Lines changed: 32 additions & 24 deletions

File tree

lib/firebase/auth.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,70 @@
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";
56

6-
// Function to track authentication state changes
7+
// 🔹 Track Authentication State Changes
78
export function onAuthStateChanged(callback: (authUser: User | null) => void) {
89
return auth.onAuthStateChanged(callback);
910
}
1011

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> {
1314
const provider = new GoogleAuthProvider();
14-
1515
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);
1817
} catch (error) {
19-
console.error('Error signing in with Google:', error);
18+
console.error("Error signing in with Google:", error);
2019
throw error;
2120
}
2221
}
2322

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+
2627
try {
2728
const result: UserCredential | null = await getRedirectResult(auth);
29+
console.log("Redirect result:", result);
2830

29-
if (!result || !result.user) return null; // No redirect result
31+
if (!result || !result.user) return null;
3032

3133
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");
3335

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
3539
const allowedEmailPattern = /^[a-zA-Z0-9]+@gecskp\.ac\.in$/;
3640
const adminOverrideEmail = "codecompass2024@gmail.com";
3741

3842
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");
4045
}
4146

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);
4449
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("/");
4654

4755
return { isAdmin };
4856
} catch (error) {
49-
console.error('Error handling Google sign-in redirect result:', error);
57+
console.error("Error handling Google sign-in redirect result:", error);
5058
return null;
5159
}
5260
}
5361

54-
// Function to sign out
62+
// 🔹 Sign Out Function
5563
export async function signOutWithGoogle(): Promise<void> {
5664
try {
57-
await auth.signOut();
65+
await signOut(auth);
5866
} catch (error) {
59-
console.error('Error signing out with Google:', error);
67+
console.error("Error signing out with Google:", error);
6068
throw error;
6169
}
6270
}

0 commit comments

Comments
 (0)