Skip to content

Commit 1a893c0

Browse files
author
Boopathi
committed
Update authentication components and add Firebase auth hook
1 parent 7e0dc55 commit 1a893c0

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

src/app/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { useToast } from "@/hooks/use-toast";
1212
import { format } from "date-fns";
1313
import { Dialog, DialogContent } from "@/components/ui/dialog";
1414
import { Loader2 } from "lucide-react";
15-
import { auth } from "@/lib/firebase";
16-
import { onAuthStateChanged, signOut, User as FirebaseUser } from "firebase/auth";
15+
import { signOut } from "firebase/auth";
16+
import { auth } from "@/hooks/use-firebase-auth";
1717
import { API_BASE_URL } from "@/lib/api";
1818

1919
// A more obvious loading animation to show while dynamic components are being downloaded.

src/components/auth/login-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Loader2 } from "lucide-react";
1212
import { useToast } from "@/hooks/use-toast";
1313
import type { UserRole } from "@/app/types";
1414
import { API_BASE_URL } from "@/lib/api";
15-
import { auth } from "@/lib/firebase";
15+
import { auth } from "@/hooks/use-firebase-auth";
1616
import {
1717
signInWithEmailAndPassword,
1818
sendPasswordResetEmail,

src/components/auth/signup-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "
1111
import { Loader2 } from "lucide-react";
1212
import { useToast } from "@/hooks/use-toast";
1313
import { API_BASE_URL } from "@/lib/api";
14-
import { auth } from "@/lib/firebase";
14+
import { auth } from "@/hooks/use-firebase-auth";
1515
import {
1616
GoogleAuthProvider,
1717
signInWithPopup

src/hooks/use-firebase-auth.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
"use client";
3+
4+
import { useState, useEffect } from 'react';
5+
import { getAuth, onAuthStateChanged, User as FirebaseUser } from 'firebase/auth';
6+
import { getFirebaseApp } from '@/lib/firebase';
7+
8+
const app = getFirebaseApp();
9+
export const auth = getAuth(app);
10+
11+
export function useFirebaseAuth() {
12+
const [user, setUser] = useState<FirebaseUser | null>(null);
13+
const [loading, setLoading] = useState(true);
14+
15+
useEffect(() => {
16+
const unsubscribe = onAuthStateChanged(auth, (user) => {
17+
setUser(user);
18+
setLoading(false);
19+
});
20+
21+
return () => unsubscribe();
22+
}, []);
23+
24+
return { user, loading };
25+
}

src/lib/firebase.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
import { initializeApp, getApps, getApp, FirebaseApp } from "firebase/app";
3-
import { getAuth, Auth } from "firebase/auth";
43

54
const firebaseConfig = {
65
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
@@ -12,8 +11,11 @@ const firebaseConfig = {
1211
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
1312
};
1413

15-
// Initialize Firebase for SSR and client-side
16-
const app: FirebaseApp = !getApps().length ? initializeApp(firebaseConfig) : getApp();
17-
const auth: Auth = getAuth(app);
18-
19-
export { app, auth };
14+
// A function to initialize Firebase App, ensuring it's a singleton.
15+
// This can be called safely from the client-side.
16+
export function getFirebaseApp(): FirebaseApp {
17+
if (getApps().length) {
18+
return getApp();
19+
}
20+
return initializeApp(firebaseConfig);
21+
}

0 commit comments

Comments
 (0)