We use the <AuthCheck/> component for cleaner routing when not logged in, See App.tsx
Will be adding create user, add items and delete items
Required
you must create a file called src/env.js and add the following code
exportconstFIREBASE_CONFIG={// YOUR FIREBASE CONFIGURATION};// NAME OF COLLECTION IN FIREBASE TO LISTexportconstFIREBASE_COLLECTION_NAME="users"// THIS IS REQUIRED FOR ANDROID// SEE - https://github.com/FirebaseExtended/reactfire/issues/228global.globalThis=window;
Code Snippets
Checking Authentication Status At Startup
Here is the app it is much cleaner to check auth status and redirect the user appropriately, all of the listening for authChangeStatus and such is all gone...
If the authCheck fails, then we use the Login component.
This is a 'hack' that is currently needed to address issues with clearing information between authenticated sessions. As stated above, this is not production ready code.
This code was pulled from the issue in github where a work around was presented.
I found that this work around did not work on the android browser because the globalThis object was undefined. To resolve this issue, I set globalThis to window in the env.js file.
exportconstFIREBASE_CONFIG={// YOUR FIREBASE CONFIGURATION};// NAME OF COLLECTION IN FIREBASE TO LISTexportconstFIREBASE_COLLECTION_NAME="users"// THIS IS REQUIRED FOR ANDROID// SEE - https://github.com/FirebaseExtended/reactfire/issues/228global.globalThis=window;
// SEE - https://github.com/FirebaseExtended/reactfire/issues/228useEffect(()=>{letmap=(globalThisasany)["_reactFirePreloadedObservables"];map&&Array.from(map.keys()).forEach((key:any)=>key.includes("firestore")&&map.delete(key));},[]);
Here we use the useAuth hook to get the auth object to call the signInWithEmailAndPassword method
constauth=useAuth();/**
* get data from form and sign the user in
*/constsignIn=async(data:any)=>{try{letr=awaitauth.signInWithEmailAndPassword(data.email,data.password);console.log(r);}catch(e){setShowErrorAlert(e.message);}};
Rendering A Collection from the Database
use the useAuth hook to get auth object for logging the user out.
use the useFirebaseApp hook to get firebaseApp collection
use the useFirestoreCollectionData to get the data collection to render.
I am starting to integrated typescript into my examples since I am seeing questions about types popping up in the forums. The IModalData is the structure of the data that is written to…