Last active
December 29, 2020 20:27
-
-
Save pena56/a08c0affac46fa1f8cf5d34bcbb05bd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
import './App.css'; | |
import { auth, googleProvider, db } from './firebaseSetting'; | |
function App() { | |
const [user, setUser] = useState(null); | |
const [todos, setTodos] = useState([]); //new | |
const signInWithGoogle = () => { | |
auth | |
.signInWithPopup(googleProvider) | |
.then((res) => { | |
setUser(res.user); | |
}) | |
.catch((err) => { | |
console.error(err); | |
}); | |
}; | |
// Fetch Todos from firestore | |
useEffect(() => { | |
const fetchTodos = () => { | |
db.collection('todos') | |
.orderBy('createdAt', 'desc') | |
.onSnapshot((snapshot) => { | |
const data = snapshot.docs.map((doc) => ({ | |
id: doc.id, | |
...doc.data(), | |
})); | |
setTodos(data); | |
}); | |
}; | |
fetchTodos(); | |
}, []); | |
return ( | |
<div className="App"> | |
{user ? ( | |
<div> | |
<h2>Welcome {user.displayName}</h2> | |
// Map through todos | |
{todos.map((todo) => ( | |
<p key={todo.id}>{todo.title}</p> | |
))} | |
</div> | |
) : ( | |
<button onClick={signInWithGoogle}>Sign In with Google</button> | |
)} | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment