added firestore config
This commit is contained in:
@@ -13,7 +13,9 @@ import {
|
||||
signOut as firebaseSignOut,
|
||||
type User,
|
||||
} from 'firebase/auth'
|
||||
import { auth, googleProvider } from '../lib/firebase'
|
||||
import { auth, googleProvider, db } from '../lib/firebase'
|
||||
import { doc, setDoc } from 'firebase/firestore'
|
||||
import { COLLECTIONS } from '../lib/firestoreConfig'
|
||||
|
||||
type AuthContextValue = {
|
||||
user: User | null
|
||||
@@ -28,9 +30,28 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [user, setUser] = useState<User | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
// Save user info to Firestore when they authenticate
|
||||
async function saveUserToFirestore(authUser: User) {
|
||||
try {
|
||||
const userRef = doc(db, COLLECTIONS.USERS, authUser.uid)
|
||||
await setDoc(userRef, {
|
||||
id: authUser.uid,
|
||||
email: authUser.email || '',
|
||||
displayName: authUser.displayName || '',
|
||||
photoURL: authUser.photoURL || '',
|
||||
lastLoginAt: Date.now(),
|
||||
}, { merge: true })
|
||||
} catch (error) {
|
||||
console.error('Error saving user to Firestore:', error)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = onAuthStateChanged(auth, (u) => {
|
||||
const unsubscribe = onAuthStateChanged(auth, async (u) => {
|
||||
setUser(u)
|
||||
if (u) {
|
||||
await saveUserToFirestore(u)
|
||||
}
|
||||
setLoading(false)
|
||||
})
|
||||
return () => unsubscribe()
|
||||
|
||||
Reference in New Issue
Block a user