Spaces:
Running
Running
Download components/FirebaseProvider.js from Leogron777/anycoder-fa44b9d5: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/spaces/Leogron777/anycoder-fa44b9d5/resolve/main/components/FirebaseProvider.js
- Command line
-
hf download hf://spaces/Leogron777/anycoder-fa44b9d5/components/FirebaseProvider.js
-
curl -L -o FirebaseProvider.js https://huggingface.co/spaces/Leogron777/anycoder-fa44b9d5/resolve/main/components/FirebaseProvider.js
2.62 kB
| import { createContext, useContext, useEffect, useState } from 'react' | |
| import { initializeApp } from 'firebase/app' | |
| import { getAuth, GoogleAuthProvider, signInWithPopup, signInWithEmailAndPassword, createUserWithEmailAndPassword, signOut } from 'firebase/auth' | |
| import { getFirestore, collection, doc, setDoc, getDoc, getDocs, query, where, orderBy, onSnapshot } from 'firebase/firestore' | |
| const FirebaseContext = createContext() | |
| const firebaseConfig = { | |
| apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY || "demo-key", | |
| authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN || "demo.firebaseapp.com", | |
| projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID || "demo-project", | |
| storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET || "demo.appspot.com", | |
| messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID || "123456789", | |
| appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID || "1:123456789:web:abc123" | |
| } | |
| const app = initializeApp(firebaseConfig) | |
| const auth = getAuth(app) | |
| const db = getFirestore(app) | |
| export function FirebaseProvider({ children }) { | |
| const [user, setUser] = useState(null) | |
| const [loading, setLoading] = useState(true) | |
| useEffect(() => { | |
| const unsubscribe = auth.onAuthStateChanged((user) => { | |
| setUser(user) | |
| setLoading(false) | |
| }) | |
| return unsubscribe | |
| }, []) | |
| const signInWithGoogle = async () => { | |
| const provider = new GoogleAuthProvider() | |
| try { | |
| await signInWithPopup(auth, provider) | |
| } catch (error) { | |
| console.error('Google sign in error:', error) | |
| } | |
| } | |
| const signInWithEmail = async (email, password) => { | |
| try { | |
| await signInWithEmailAndPassword(auth, email, password) | |
| } catch (error) { | |
| console.error('Email sign in error:', error) | |
| throw error | |
| } | |
| } | |
| const signUpWithEmail = async (email, password) => { | |
| try { | |
| await createUserWithEmailAndPassword(auth, email, password) | |
| } catch (error) { | |
| console.error('Email sign up error:', error) | |
| throw error | |
| } | |
| } | |
| const logout = async () => { | |
| try { | |
| await signOut(auth) | |
| } catch (error) { | |
| console.error('Logout error:', error) | |
| } | |
| } | |
| const value = { | |
| user, | |
| loading, | |
| auth, | |
| db, | |
| signInWithGoogle, | |
| signInWithEmail, | |
| signUpWithEmail, | |
| logout | |
| } | |
| return ( | |
| <FirebaseContext.Provider value={value}> | |
| {children} | |
| </FirebaseContext.Provider> | |
| ) | |
| } | |
| export const useFirebase = () => { | |
| const context = useContext(FirebaseContext) | |
| if (!context) { | |
| throw new Error('useFirebase must be used within FirebaseProvider') | |
| } | |
| return context | |
| } |