Last active
March 19, 2023 17:33
-
-
Save RayanAbid/df4329cbe92e2a9e71fbb2cd0eada193 to your computer and use it in GitHub Desktop.
Some useful firebase function that I use regularly
This file contains hidden or 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 the functions you need from the SDKs you need | |
import * as firebase from "firebase"; | |
import "firebase/auth"; | |
import "firebase/firestore"; | |
// import "firebase/auth"; | |
// import "firebase/analytics"; | |
// TODO: Add SDKs for Firebase products that you want to use | |
// https://firebase.google.com/docs/web/setup#available-libraries | |
// Your web app's Firebase configuration | |
// For Firebase JS SDK v7.20.0 and later, measurementId is optional | |
const firebaseConfig = { | |
apiKey: YOUR_KEY, | |
authDomain: YOUR_KEY, | |
projectId: YOUR_KEY, | |
storageBucket: YOUR_KEY, | |
messagingSenderId: YOUR_KEY, | |
appId: YOUR_KEY, | |
measurementId: YOUR_KEY, | |
}; | |
// Initialize Firebase | |
let app; | |
if (firebase.apps.length === 0) { | |
app = firebase.initializeApp(firebaseConfig); | |
} else { | |
app = firebase.app(); | |
} | |
const auth = firebase.auth(); | |
const db = firebase.firestore(); | |
export { auth, db }; |
This file contains hidden or 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 { auth, db } from "../utils/firebase.js"; | |
// used for logging in | |
const handleLogin = async () => { | |
await auth | |
.signInWithEmailAndPassword(email, password) | |
.then((userCredential) => { | |
// Signed in | |
var user = userCredential.user; | |
console.log("test the user:", user); | |
// ... | |
}) | |
.catch((error) => { | |
var errorCode = error.code; | |
var errorMessage = error.message; | |
console.error("test the user:", errorCode, errorMessage); | |
// .. | |
}); | |
}; | |
// used for creating account | |
const handleSignup = async () => { | |
await auth | |
.createUserWithEmailAndPassword(email, password) | |
.then(async (userCredential) => { | |
console.log("email, password", email, password); | |
// Signed in | |
console.log("tete", userCredential); | |
var user = userCredential; | |
await db.collection("users").doc(user.user.uid).set({ | |
email: email.toLowerCase().trim(), | |
uid: user.user.uid, | |
name: "test", | |
}); | |
console.log("test the user:", user); | |
// ... | |
}) | |
.catch((error) => { | |
var errorCode = error.code; | |
var errorMessage = error.message; | |
console.error("test the user:", errorCode, errorMessage); | |
// .. | |
}); | |
}; | |
// get single user data | |
const getUserData = async () => { | |
console.log("auth.currentUser.uid", auth.currentUser.uid); | |
db.collection("users") | |
.doc(auth.currentUser.uid) | |
.get() | |
.then((data) => { | |
if (data.exists) { | |
console.log("Document data:", data.data()); | |
setUser(data?.data()); | |
} else { | |
// doc.data() will be undefined in this case | |
console.log("No such document!"); | |
} | |
}) | |
.catch((error) => { | |
console.log("Error getting document:", error); | |
}); | |
}; | |
// check if login than navigate | |
useEffect(() => { | |
auth.onAuthStateChanged((user) => { | |
if (user) { | |
// User is signed in, see docs for a list of available properties | |
// https://firebase.google.com/docs/reference/js/firebase.User | |
var uid = user.uid; | |
navigation.reset({ index: 0, routes: [{ name: "Home" }] }); | |
// ... | |
} else { | |
// User is signed out | |
// ... | |
} | |
}); | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment