Intro To Firebase ReactFire v4 - Login, Logout Create Account And Protected Routes
Tags: firebase, react, reactfire, ionic
Published: 2022-03-01 23:49:17

## Overview
This is a quick walkthrough of a code example using [ReactFire](https://firebaseopensource.com/projects/firebaseextended/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](https://firebaseopensource.com/projects/firebaseextended/reactfire/).

This is an updated version of a previously released reactfire intro application and [video](https://youtu.be/xOVqoVuY-5M) 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]( https://github.com/aaronksaunders/quick-intro-reactfire-v4)

The code uses [Ionic Framework](https://ionicframework.com/docs/react) for UI but the code is react so it should work in all reactjs based applications

##Video
{% youtube https://youtu.be/-OIwhD2GE7g %}

## 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](https://stackoverflow.com/questions/71189584/issue-starting-firebase-emulator-node-17)

### Login Code
We need the `getAuth` hook from reactFire
```jsx
const auth = getAuth();
```
then we use the auth object to make the call to sign in with the user credentials

```jsx
  const doSignIn = () => {
    signInWithEmailAndPassword(auth, email, password)
      .then((userCredential) => {
        // Signed in
        const user = userCredential.user;
        console.log(user);
        history.push("/home");
        return true;
      })
      .catch(async (error) => {
        const errorCode = error.code;
        const errorMessage = error.message;

        await alert({
          header: "Error Signing In",
          message: errorMessage,
          buttons: ["OK"],
        });
      });
  };
```
### Create Account Code
We need the `getAuth` hook from reactFire
```jsx
const auth = getAuth();
```
then we use the auth object to make the call to create the user account using the user credentials

```jsx
  const doCreateAccount = () => {
    createUserWithEmailAndPassword(auth, email, password)
      .then((userCredential) => {
        // Signed in
        const user = userCredential.user;
        console.log(user);
        history.replace("/");
        return true;
      })
      .catch(async (error) => {
        const errorCode = error.code;
        const errorMessage = error.message;

        await alert({
          header: "Error Creating Account",
          message: errorMessage,
          buttons: ["OK"],
        });
      });
  };
```
### Sign Out Code
We need the `getAuth` hook from reactFire
```jsx
const auth = getAuth();
```
then we use the auth object to make the call to sign the user out

```jsx
<IonButton
    onClick={async () => {
        await signOut(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`
```jsx
  return (
    <IonApp>
      <AuthProvider sdk={auth}>
        <FirestoreProvider sdk={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
```jsx
  <IonReactRouter>
    <IonRouterOutlet>
      <Route path="/" exact={true}>
        <Redirect to="/home" />
      </Route>
      <PrivateRoute path="/home" exact={true}>
        <Home />
      </PrivateRoute>
      <Route path="/login" exact={true}>
        <Login />
      </Route>
      <Route path="/create-account" exact={true}>
        <CreateAccount />
      </Route>
    </IonRouterOutlet>
  </IonReactRouter>
```

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
```jsx
// A wrapper for <Route> that redirects to the login
// screen if you're not yet authenticated.
export const PrivateRoute = ({
  children,
  location,
  ...rest
}: React.PropsWithChildren<any>) => {
  const { status, data: signInCheckResult } = useSigninCheck();
  console.log(signInCheckResult);
  debugger;
  if (status === "loading") {
    return <IonLoading isOpen={status === "loading"} />;
  }

  return (
    <Route
      {...rest}
      render={({ location }) =>
        signInCheckResult.signedIn === true ? (
          children
        ) : (
          <Redirect
            to={{
              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.

```jsx
  <AuthWrapper fallback={<AuthRoute />}>
    <Route path="/" exact={true}>
      <Redirect to="/home" />
    </Route>
    <Route path="/home" exact={true}>
      <Home />
    </Route>
  </AuthWrapper>
```

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.
```jsx
const AuthRoute = () => {
  return (
    <Switch>
      <Route path="/login" exact={true}>
        <Login />
      </Route>
      <Route path="/create-account" exact={true}>
        <CreateAccount />
      </Route>
      <Route path="*" exact={true}>
        <Redirect to="/login" />
      </Route>
    </Switch>
  );
};
```

From the ReactFire Example Code, see this is in `AppAuthWrapper.tsx`. The AuthWrapper code is from the [reactfire repo](https://github.com/FirebaseExtended/reactfire) to account for the removal of `AuthCheck` component
```jsx
export const AuthWrapper = ({
  children,
  fallback,
}: React.PropsWithChildren<{ fallback: JSX.Element }>): JSX.Element => {
  const { status, data: signInCheckResult } = useSigninCheck();
  console.log(signInCheckResult);

  if (!children) {
    throw new Error("Children must be provided");
  }
  if (status === "loading") {
    return <IonLoading isOpen={status === "loading"} />;
  } else if (signInCheckResult.signedIn === true) {
    return children as JSX.Element;
  }

  return fallback;
};

```
### Using Capacitor
when using capacitor you will need to initialize auth differently.

See bug - https://github.com/firebase/firebase-js-sdk/issues/5552#issuecomment-929580662
```js
  const auth = initializeAuth(app, {
    persistence: indexedDBLocalPersistence
  });

  // browser only
  // const auth = getAuth(app);
```


{% github aaronksaunders/quick-intro-reactfire-v4 %}

---
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