Skip to content

Commit be84656

Browse files
committed
temperoily you can add new project without loggin in due to bug of auth
1 parent 47188e1 commit be84656

File tree

3 files changed

+59
-75
lines changed

3 files changed

+59
-75
lines changed

app/login/page.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
11
"use client";
22

3-
import { useState } from "react";
4-
import { signInWithGoogle } from "@/lib/firebase/auth";
3+
import { useState, useEffect } from "react";
4+
import { signInWithGoogle, handleRedirectResult } from "@/lib/firebase/auth";
55
import { useRouter } from "next/navigation";
66

77
export default function AuthPage() {
88
const [error, setError] = useState<string | null>(null);
9+
// Removed unused isAdmin state
910
const router = useRouter();
1011

12+
// 🔹 Handle Login Click
1113
const handleLoginWithGoogle = async () => {
1214
try {
13-
const result = await signInWithGoogle();
14-
if (result?.isAdmin) {
15-
router.push("/admin");
16-
} else {
17-
router.push("/");
18-
}
15+
await signInWithGoogle();
1916
} catch (err) {
20-
console.error("Failed to log in with Google:", err);
21-
setError(err instanceof Error ? err.message : "Failed to log in with Google");
17+
console.error("Failed to log in:", err);
18+
setError(err instanceof Error ? err.message : "Login failed.");
2219
}
2320
};
2421

22+
// 🔹 Check Login Result After Redirect
23+
useEffect(() => {
24+
async function checkLogin() {
25+
const result = await handleRedirectResult();
26+
if (result) {
27+
// Removed setIsAdmin as isAdmin state is no longer used
28+
router.push(result.isAdmin ? "/admin" : "/");
29+
}
30+
}
31+
checkLogin();
32+
}, [router]);
33+
2534
return (
26-
<div className="flex items-center justify-center min-h-screen bg-black dark:bg-gray-950 transition-colors duration-300">
27-
<div className="w-full max-w-md p-6 space-y-6 bg-gray-500 dark:bg-gray-900 shadow-lg rounded-2xl transition-all">
35+
<div className="flex items-center justify-center min-h-screen bg-black dark:bg-gray-950">
36+
<div className="w-full max-w-md p-6 space-y-6 bg-gray-500 dark:bg-gray-900 shadow-lg rounded-2xl">
2837
<h2 className="text-2xl font-bold text-gray-900 dark:text-gray-200 text-center">
2938
Login to your account
3039
</h2>
3140

3241
<button
3342
onClick={handleLoginWithGoogle}
34-
aria-label="Login with Google"
35-
type="button"
3643
className="flex items-center justify-center w-full p-3 space-x-3 border rounded-lg
37-
bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700
38-
dark:border-gray-700 focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-all"
44+
bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700"
3945
>
4046
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" className="w-6 h-6 fill-current text-gray-700 dark:text-gray-200">
4147
<path d="M16.318 13.714v5.484h9.078c-0.37 2.354-2.745 6.901-9.078 6.901-5.458 0-9.917-4.521-9.917-10.099s4.458-10.099 9.917-10.099c3.109 0 5.193 1.318 6.38 2.464l4.339-4.182c-2.786-2.599-6.396-4.182-10.719-4.182-8.844 0-16 7.151-16 16s7.156 16 16 16c9.234 0 15.365-6.49 15.365-15.635 0-1.052-0.115-1.854-0.255-2.651z"></path>

components/repeto/AddProjectFAB.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
"use client";
22

3-
import { useEffect, useState } from "react";
4-
import { User } from "firebase/auth"; // Adjust the import path based on your Firebase setup
53
import { useRouter } from "next/navigation";
64
import { Plus } from "lucide-react";
7-
import { onAuthStateChanged } from "@/lib/firebase/auth";
85

96
const AddProjectFAB = () => {
107
const router = useRouter();
11-
const [user, setUser] = useState<User | null>(null);
12-
13-
useEffect(() => {
14-
const unsubscribe = onAuthStateChanged((currentUser) => {
15-
setUser(currentUser);
16-
});
17-
return () => unsubscribe();
18-
}, []);
198

209
const handleClick = () => {
21-
if (user) {
22-
router.push("/add-project");
23-
} else {
24-
router.push("/login"); // Redirect to login if not authenticated
25-
}
10+
router.push("/add-project"); // Directly navigate to add-project
2611
};
2712

2813
return (

lib/firebase/auth.ts

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,63 @@
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";
35
import { doc, getDoc } from "firebase/firestore";
46
import { auth, firestore } from "./config";
5-
import { NavigateFunction } from "react-router-dom";
67

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)
139
export async function signInWithGoogle(): Promise<void> {
1410
const provider = new GoogleAuthProvider();
1511
try {
16-
await signInWithRedirect(auth, provider);
12+
await signInWithRedirect(auth, provider);
1713
} 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;
2016
}
2117
}
2218

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> {
2721
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;
3224

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");
3527

36-
console.log("User logged in:", user.email);
28+
console.log("User logged in:", user.email);
3729

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";
4133

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+
}
4638

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";
5143

52-
// ✅ Redirect to home page after successful login
53-
navigate("/");
44+
// ✅ Store Admin Status in Local Storage
45+
localStorage.setItem("isAdmin", isAdmin.toString());
5446

55-
return { isAdmin };
47+
return { isAdmin };
5648
} 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;
5951
}
6052
}
6153

62-
// 🔹 Sign Out Function
54+
// 🔹 Sign Out
6355
export async function signOutWithGoogle(): Promise<void> {
6456
try {
65-
await signOut(auth);
57+
await signOut(auth);
58+
localStorage.removeItem("isAdmin"); // Clear admin state
6659
} catch (error) {
67-
console.error("Error signing out with Google:", error);
68-
throw error;
60+
console.error("Error signing out:", error);
61+
throw error;
6962
}
7063
}

0 commit comments

Comments
 (0)