Skip to content

Instantly share code, notes, and snippets.

@YousraBD
Last active April 30, 2021 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YousraBD/e46e0663b3dc649cdfdbb78e645b6a92 to your computer and use it in GitHub Desktop.
Save YousraBD/e46e0663b3dc649cdfdbb78e645b6a92 to your computer and use it in GitHub Desktop.
firebase-debug.log*
firestore-debug.log*
ui-debug.log*
// 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;
// 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;
try {
await auth().signInWithPopup(provider);
const user = await auth().currentUser;
setUser(user);
} catch(e) {}
// 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 };
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;
{
"emulators": "firebase emulators:start --import=data --export-on-exit",
}
React.useEffect(() => {
return auth().onAuthStateChanged((user) => {
if (user) {
setUser(user);
} else {
setUser(null);
}
});
}, []);
yarn add firebase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment