diff --git a/src/App.jsx b/src/App.jsx index a02f45b..032bc57 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,7 +1,11 @@ import React, { useState, useEffect } from 'react'; import './App.css'; + +import { useAuthState, signInWithGoogle, firebaseSignOut } from './utilities/firebase'; + import { findCafes } from './utilities/findCafes'; + function App() { const [reviews, setReviews] = useState([]); const [availability, setAvailability] = useState({}); @@ -13,6 +17,19 @@ function App() { const [dist, setDist] = useState(''); const [cafes, setCafes] = useState([]); + const [user] = useAuthState(); + + + const handleSignIn = async () => { + try { + await signInWithGoogle(); // Sign in with Google + } catch (error) { + console.error('Error signing in:', error); + } + }; + + const handleLogout = () => firebaseSignOut(); + useEffect(() => { }, []); @@ -73,7 +90,18 @@ function App() {

CafeWay

- + {user ? ( + <> + +

Welcome, {user.email}

+ + ) : ( + + )}
{ + signInWithPopup(auth, provider) + .then((result) => { + console.log('User signed in:', result.user); + }) + .catch((error) => { + console.error('Error signing in with Google:', error); + }); + }; + +export const firebaseSignOut = () => signOut(auth); + +export const useAuthState = () => { + const [user, setUser] = useState(null); + + useEffect(() => { + const unsubscribe = onAuthStateChanged(auth, (user) => setUser(user)); + return () => unsubscribe(); + }, []); + + return [user]; +}; + +export const useDbData = (path) => { + const [data, setData] = useState(); + const [error, setError] = useState(null); + + useEffect(() => { + const dbRef = ref(database, path); + const unsubscribe = onValue( + dbRef, + (snapshot) => setData(snapshot.val()), + (error) => setError(error) + ); + return () => unsubscribe(); + }, [path]); + + return [data, error]; +}; + +export const useDbUpdate = (path) => { + const [result, setResult] = useState(); + + const updateData = useCallback( + (value) => { + update(ref(database, path), value) + .then(() => setResult({ message: "Update successful", timestamp: Date.now() })) + .catch((error) => setResult({ error, message: error.message })); + }, + [path] + ); + + return [updateData, result]; +}; \ No newline at end of file