Last active
April 30, 2021 11:58
-
-
Save YousraBD/e46e0663b3dc649cdfdbb78e645b6a92 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
firebase-debug.log* | |
firestore-debug.log* | |
ui-debug.log* |
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
// src/App.js | |
import React from "react"; | |
import { db } from "./firebase"; | |
function App() { | |
db.doc("hello/world").set({ hello: "world" }); | |
return <div />; | |
} | |
export default App; |
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
// src/App.js | |
import React from 'react'; | |
import { db } from './firebase'; | |
function App() { | |
const [data, setData] = React.useState(); | |
React.useEffect(() => { | |
db.doc('people/me') | |
.get() | |
.then((data) => setData(data.data())); | |
}, []); | |
return <p>{data?.hungry ? "I'm hungry" : "I'm full"}!</p>; | |
} | |
export default App; |
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
try { | |
await auth().signInWithPopup(provider); | |
const user = await auth().currentUser; | |
setUser(user); | |
} catch(e) {} |
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
// src/firebase.js | |
import firebase from 'firebase/app'; | |
import 'firebase/auth'; | |
import 'firebase/firestore'; | |
// TODO: Use a configuration object | |
firebaseApp.initializeApp({ | |
projectId: '', | |
appId: '', | |
databaseURL: '', | |
storageBucket: '', | |
locationId: '', | |
apiKey: '', | |
authDomain: '', | |
messagingSenderId: '', | |
}); | |
const db = firebase.firestore(); | |
const auth = firebase.auth; | |
// eslint-disable-next-line no-restricted-globals | |
if (location.hostname === 'localhost') { | |
db.useEmulator('localhost', 8080); | |
auth().useEmulator('http://localhost:9099/', { disableWarnings: true }); | |
} | |
export default firebase; | |
export { db, auth }; |
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 React from 'react'; | |
import { auth } from './firebase'; | |
function Login() { | |
const login = async () => { | |
const provider = new auth.GoogleAuthProvider(); | |
try { | |
await auth().signInWithPopup(provider); | |
} catch (e) {} | |
}; | |
return <button onClick={login}>login</button>; | |
} | |
export default Login; |
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
{ | |
"emulators": "firebase emulators:start --import=data --export-on-exit", | |
} |
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
React.useEffect(() => { | |
return auth().onAuthStateChanged((user) => { | |
if (user) { | |
setUser(user); | |
} else { | |
setUser(null); | |
} | |
}); | |
}, []); |
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
yarn add firebase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment