Intro To Firebase ReactFire v4 - Login, Logout Create Account And Protected Routes
Aaron K Saunders
Posted on March 1, 2022
Overview
This is a quick walkthrough of a code example using ReactFire v4 in an application. The application supports login, logout, create an account, and protected routes. We also walk through two approaches for protecting routes since the AuthCheck component that existed in v3 no longer exists in v4 of ReactFire.
This is an updated version of a previously released reactfire intro application and video that was working with v3; this new code will work with v4.
The source code for this video and the two approaches for protecting routes is available in the github repo
The code uses Ionic Framework for UI but the code is react so it should work in all reactjs based applications
Video
Code
I am using Firebase Emulator in my project, if you are going to do the same, be sure you are using node v16 otherwise you will run into issues Issue In StackOverflow
Login Code
We need the getAuth hook from reactFire
constauth=getAuth();
then we use the auth object to make the call to sign in with the user credentials
constdoSignIn=()=>{signInWithEmailAndPassword(auth,email,password).then((userCredential)=>{// Signed inconstuser=userCredential.user;console.log(user);history.push("/home");returntrue;}).catch(async (error)=>{consterrorCode=error.code;consterrorMessage=error.message;awaitalert({header:"Error Signing In",message:errorMessage,buttons:["OK"],});});};
Create Account Code
We need the getAuth hook from reactFire
constauth=getAuth();
then we use the auth object to make the call to create the user account using the user credentials
constdoCreateAccount=()=>{createUserWithEmailAndPassword(auth,email,password).then((userCredential)=>{// Signed inconstuser=userCredential.user;console.log(user);history.replace("/");returntrue;}).catch(async (error)=>{consterrorCode=error.code;consterrorMessage=error.message;awaitalert({header:"Error Creating Account",message:errorMessage,buttons:["OK"],});});};
Sign Out Code
We need the getAuth hook from reactFire
constauth=getAuth();
then we use the auth object to make the call to sign the user out
<IonButtononClick={async ()=>{awaitsignOut(auth);history.replace("/login");}}>
SIGN OUT
</IonButton>
Two Approaches For Checking For Auth User
In both cases you will need to wrap all of the Routes with the AuthProvider and the FirestoreProvider
return (<IonApp><AuthProvidersdk={auth}><FirestoreProvidersdk={firestoreDatabase}>
... Routes Go Here ...
</FirestoreProvider></AuthProvider></IonApp>);};
PrivateRoute Component
Using the PrivateRoute Component, we setup our Router using the PrivateRoute component instead of the Route component for protected routes.
Note here we need to use the Ionic specific Router IonReactRouter but it can be replaced with ReactRouter in a react application
for this to work with IonicReactRouter, I had to remove the location from being passed in to the redirect as state. IonicRouter doesnt support Switch, so the thing just kept looping
// A wrapper for <Route> that redirects to the login// screen if you're not yet authenticated.exportconstPrivateRoute=({children,location,...rest}:React.PropsWithChildren<any>)=>{const{status,data:signInCheckResult}=useSigninCheck();console.log(signInCheckResult);debugger;if (status==="loading"){return<IonLoadingisOpen={status==="loading"}/>;}return (<Route{...rest}render={({location})=>signInCheckResult.signedIn===true?(children):(<Redirectto={{pathname:"/login",}}/>)}/>);};
AuthWrapper Component
We need to set up the Router a bit differently here. You can see that we wrap all of our routes with AuthWrapper similar to what we did in v3 using the AuthCheck component.
We also need to point to the fallback route if there is no authenticated user. We have created a separate component that includes all of the non protected routes.
Note when using IonicReactRouter this code will not work properly since IonicReactRouter doesn't support Switch at the top level.
From the ReactFire Example Code, see this is in AppAuthWrapper.tsx. The AuthWrapper code is from the reactfire repo to account for the removal of AuthCheck component
exportconstAuthWrapper=({children,fallback,}:React.PropsWithChildren<{fallback:JSX.Element}>):JSX.Element=>{const{status,data:signInCheckResult}=useSigninCheck();console.log(signInCheckResult);if (!children){thrownewError("Children must be provided");}if (status==="loading"){return<IonLoadingisOpen={status==="loading"}/>;}elseif (signInCheckResult.signedIn===true){returnchildrenasJSX.Element;}returnfallback;};
Using Capacitor
when using capacitor you will need to initialize auth differently.
updated previously released reactfire intro application to work with v4
current has auth and create account, will add some CRUD functionality soon
used Ionic Framework for UI but the code is react so it should work in all cases
Two Approaches For Checking For Auth User
From the react router documentation..
for this to work with IonicReactRouter, I had to remove the location from being passed in to the redirect as state. IonicRouter doesnt support Switch, so the thing just kept looping
// A wrapper for <Route> that redirects to the login// screen if you're not yet authenticated.exportconstPrivateRoute=({
children,
location,
...rest}: React.PropsWithChildren<any>)=>{const{ status,data: signInCheckResult}=useSigninCheck(
Looking for support with your cross-platform mobile solution using Ionic Framework? Contact Me at my company Clearly Innovative Inc for a free 30-minute Consultation www.clearlyinnovative.com