firebase-react-provider
This library is a React component (provider) using React Hooks and requires some dependencies:
- react (>=16.13.0)
- firebase (>=7.23.0 recommended)
Although the library is named FirebaseProvider
, you should be aware this library is using: firebase/app, firebase/auth. If you are going to use other parts of firebase (i.e. storage or firestore), you can import { firebase }
from firebase-react-provider
and then dynamically import the extensions. Do this prior to using the FirebaseProvider in your app and within the scope of the same module.
// firebase here is the equivalent of the 'firebase/app' import;;;
Setting up your project with FirebaseProvider
npm install firebase firebase-react-provider
Or
yarn add firebase firebase-react-provider
Once the dependencies are installed, you can now setup the react app to use the provider and you are ready to import the hooks in your components to access the items you need.
; const config = apiKey: "AIza..................................." authDomain: "your-app-name.firebaseapp.com" databaseURL: "https://your-app-name.firebaseio.com" projectId: "your-app-name" storageBucket: "your-app-name.appspot.com" ReactDOM;
FirebaseProvider also allows for multiple apps and would be used by wrapping the providers.
; const config = apiKey: "AIza..................................." authDomain: "default-app-name.firebaseapp.com" databaseURL: "https://default-app-name.firebaseio.com" projectId: "default-app-name" storageBucket: "default-app-name.appspot.com" const adminConfig = apiKey: "PXiFa................................." authDomain: "admin-app-name.firebaseapp.com" databaseURL: "https://admin-app-name.firebaseio.com" projectId: "admin-app-name" storageBucket: "admin-app-name.appspot.com" ReactDOM;
useFirebaseUser (user)
The hook value is the current user logged into the app. The user is null
when the user isn't logged into the app and is the value of the logged in user when the state changes.
See: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#currentuser
This hook has observability based on the auth(app).onAuthStateChanged
listener.
See: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onauthstatechanged
The example below to see when a user login is resolved for a persisted login.
=> { const user = ; React; return <div>`User: `</div>}
useFirebaseAuth ({ appAuth, authConstructor })
The hook value is our auth for the app instantiated (firebase.auth(app)). We might want to setup the auth for a user to be persistent for this session only. See: https://firebase.google.com/docs/auth/web/auth-state-persistence
Note: In the example, we use the google auth provider to keep them logged in until they logout so we would use LOCAL persistence.
Below is an example:
/* const name = "app-name" // name used in the provider or optional const { authApp, authConstructor } = useFirebaseAuth(name || undefined) authApp = firebase.auth(app) authConstructor = firebase.auth*/;; const LoggedInUser = children isLoggedIn = Booleanfalse onClick const LogoutButton = { return <button onClick=onClick>Logout</button>; }; if typeof onClick !== "function" throw Error"LoggedInUser must pass onClick"; return isLoggedIn ? <LogoutButton onClick=onClick /> : null;; const LoggedOutUser = isLoggedIn = Booleanfalse onClick const LoginButton = { return <button onClick=onClick>Login</button>; }; if typeof onClick !== "function" throw Error"LoggedOutUser must pass onClick"; return !isLoggedIn ? <LoginButton onClick=onClick /> : null;; { const appAuth authConstructor = ; const user = ; const userName setUserName = React; const previousUserName = ; const previousUser = ; const inProcess setInProcess = React; { ; event; const button = eventcurrentTarget; buttondisabled = true; buttontextContent = "Logging Out..."; return appAuth ; } { ; event; const button = eventcurrentTarget; buttondisabled = true; buttontextContent = "Logging In..."; appAuth ; } React; React; return <div style= height: "200px" > inProcess || inProcess === null ? <div>Waiting...</div> : <div> <div>children</div> <LoggedInUser isLoggedIn=!!userName onClick=handleLogout /> <LoggedOutUser isLoggedIn=!!userName onClick=handleLogin /> </div> </div> ;}
useFirebaseApp(/_ name of your app if assigned in the provider _/) ([app, changeApp]);
The value of app is firebase.app(name) from the provider. Use changeApp(name)
to get the app instance from another provider being used.
Below is a component using the hook to get the app instance from the provider.
=> { const app setApp = ; React; return <div>`App: `</div>}