2.0.2 • Published 6 years ago

react-firestarter v2.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

react-firestarter

Firebase Components for React

NPM styled with prettier Build Status Coverage Status

Install

npm install --save react-firestarter

Usage

Authentication

import React from 'react';

import { AuthProvider, Authenticator } from 'react-firestarter';

function Auth() {
  return (
    <AuthProvider fireauth={firebase.auth()}>
      <Authenticator onSuccess={onSuccess} onError={onError}>
        {({ isAuthenticated, signup, login, logout }) => {
          if (isAuthenticated) {
            return <button onClick={logout}>Logout</button>;
          } else {
            return (
              <div>
                <button onClick={signup}>Signup</button>
                <button onClick={login}>Login</button>
              </div>
            );
          }
        }}
      </Authenticator>
    </AuthProvider>
  );
}

Firestore

import React from 'react';

import { FirestoreProvider, Collection, Document } from 'react-firestarter';

function Firestore() {
  return (
    <FirestoreProvider firestore={firebase.firestore()}>
      <Collection name="todos">
        {({ isLoading, todos })} => {
          if (isLoading) {
            return <div>Loading</div>;
          } else {
            (
              todos.map(todo => {
                const { id, text } = todo;

                return (
                  <Document key={id} id={id} collection="todos">
                    {({ remove })} => {
                      <div>
                        <span>{text}</span>
                        <span>
                          <button onClick={remove}>X</button>
                        </span>
                      </div>
                    }
                  </Document>
                );
              });
            )
          }
        }
      </Collection>
    </FirestoreProvider>
  );
}

Render Methods and Props

AuthProvider

NameTypeDefaultDescription
fireauthobjectrequiredyour own initialized firebase authentication module
verifyByEmailbooltrueif true, users will be required to verify their identity through an email upon signup

Authenticator

NameTypeDefaultDescription
onSuccessfunccallback for successful login or signup
onErrorfunccallback for failed login or signup

An instantiated Authenticator will receive the following:

NameTypeDescription
isAuthenticatedboollets us know if we have an authenticated user
isAuthenticatingboollets us know if a user is in the process of authenticating
redirectToReferrerbooluseful for redirecting an authenticated user to original route they landed upon
getCurrentUserfuncreturns our authenticated user
signupfuncsigns a user up; will send a verification email unless verifyByEmail is set to false in AuthProvider; takes two arguments - the first is the user to signup, and the second is a context argument which will be returned to the callback method
loginfunclogs a user in; will not succeed if the user has not and should verify their identity via email; takes two arguments - the first is the user to login, and the second is a context argument which will be returned to the callback method
logoutfunclogs a user out

FirestoreProvider

NameTypeDefaultDescription
firestoreobjectrequiredyour own initialized firebase firestore module

Collection

NameTypeDefaultDescription
namestringrequiredname of the firestore collection
onErrorfunccallback for successful fetch of collection
realtimebooltruelisten for realtime snapshot updates

An instantiated Collection will receive the following:

NameTypeDescription
isLoadingboollets us know if our document collection is in the process of loading
[collectionName]arraycollection of documents; contains an id and the result of doc.data(); the prop name is the same as the name prop passed into Collection

Document

NameTypeDefaultDescription
collectionstringrequiredname of the firestore collection the document belongs to
idanyid of the document; required for all methods except add
onSuccessfunccallback for successful execution of add, remove, update
onErrorfunccallback for failed execution of add, remove, update, and non-realtime fetch
realtimeboolfalselisten for realtime snapshot updates
fetchboolfalseget a document or listen for realtime snapshot updates

An instantiated Document will receive the following:

NameTypeDescription
addfuncadds a document; takes two arguments - the first is the item to add, and the second is a context argument which will be returned to the callback method
removefuncremoves a document; takes one argument - a context which will be returned to the callback method
updatefuncupdates a document; takes two arguments - the first is the date to update, and the second is a context argument which will be returned to the callback method
docobjecta single document object; contains the result of doc.data()
isLoadingboollets us know if our document is in the process of loading

License

MIT © jmpolitzer