Last active
September 14, 2023 12:26
-
-
Save prankush-tech/199bb7c3d6d289cfe939468379d23503 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 { initializeApp } from 'firebase/app'; | |
import { getFirestore, doc, getDoc, setDoc } from 'firebase/firestore'; | |
import { | |
getAuth, | |
signInWithRedirect, | |
signInWithPopup, | |
GoogleAuthProvider, | |
createUserWithEmailAndPassword, | |
signInWithEmailAndPassword, | |
signOut, | |
onAuthStateChanged | |
} from 'firebase/auth'; | |
const firebaseConfig = { | |
apiKey: '', | |
authDomain: '', | |
projectId: '', | |
storageBucket: '', | |
messagingSenderId: '', | |
appId: '' | |
}; | |
initializeApp(firebaseConfig); | |
const googleProvider = new GoogleAuthProvider(); | |
googleProvider.setCustomParameters({ | |
prompt: 'select_account' | |
}); | |
export const auth = getAuth(); | |
export const signInWithGooglePopUp = () => signInWithPopup(auth, googleProvider); | |
export const signInWithGoogleRedirect =()=> signInWithRedirect(auth, googleProvider); | |
export const db = getFirestore(); | |
export const createUserDocumentFromAuth = async (userAuth ,additionalinformations={}) => { | |
if(!userAuth) return; | |
const userDocRef = doc(db, 'users', userAuth.uid); | |
// console.log(userDocRef); | |
//although we dont have any user data yet, we can create a user document with the setDoc method and google firebase does respond back with a NULL DOC REFERENCE | |
const userSnapshot = await getDoc(userDocRef); | |
// console.log(userSnapshot); | |
// console.log(userSnapshot.exists()); | |
//false | |
//as the data is not present in firebase YET | |
//cause we have an empty collection in Firebase | |
if(!userSnapshot.exists()) { | |
const {displayName,email} =userAuth | |
const createdAt = new Date() ; | |
try{ | |
await setDoc(userDocRef,{ | |
displayName, | |
email, | |
createdAt, | |
...additionalinformations, | |
}) | |
} | |
catch(error) | |
{ | |
console.log("EROOR",error.message); | |
} | |
} | |
return userDocRef; | |
}; | |
//Email Password | |
export const createAuthUserWithEmailPassword = async (email,password) => { | |
if(!email || !password) { | |
return; | |
} | |
return await createUserWithEmailAndPassword(auth, email, password) | |
} | |
export const signInAuthUserWithEmailAndPassword = async (email,password) => { | |
if(!email || !password) { | |
return; | |
} | |
return await signInWithEmailAndPassword(auth, email, password) | |
} | |
//signout | |
export const signOutUser = async ()=>{ | |
return await signOut(auth); | |
} | |
//tracks the auth-state change for token | |
export const onAuthStateChangeListener = (callback)=>{ | |
return onAuthStateChanged(auth,callback) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment