• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Higher-Order Component for integrating Firebase Authentication methods with a React Component through props

react-with-firebase-auth

NPM JavaScript Style Guide Build Status codecov bundlephobia bundlephobia devdependencies peerdependencies Greenkeeper badge

Higher Order Component for integrating Firebase with a React Component

This library makes a withFirebaseAuth() function available to you.

Signature

type FirebaseAuthProps = {
  firebaseAppAuth: firebase.auth.Auth,
  providers?: {
    googleProvider?: firebase.auth.GoogleAuthProvider_Instance;
    facebookProvider?: firebase.auth.FacebookAuthProvider_Instance;
    twitterProvider?: firebase.auth.TwitterAuthProvider_Instance;
    githubProvider?:  firebase.auth.GithubAuthProvider_Instance;
  }
};

withFirebaseAuth<P>(authProps: FirebaseAuthProps) =>
  createComponentWithAuth(WrappedComponent: React.ComponentType<P>) =>
    React.ComponentType

Props Provided

type WrappedComponentProps = {
  signInWithEmailAndPassword: (email: string, password: string) => void;
  createUserWithEmailAndPassword: (email: string, password: string) => void;
  signInWithGoogle: () => void;
  signInWithFacebook: () => void;
  signInWithGithub: () => void;
  signInWithTwitter: () => void;
  signInWithPhoneNumber: (
    phoneNumber: string,
    applicationVerifier: firebase.auth.ApplicationVerifier,
  ) => void;
  signInAnonymously: () => void;
  signOut: () => void;
  setError: (error: string) => void;
  user?: firebase.User | null;
  error?: string;
  loading: boolean;
};

Usage

Install it:

npm install --save react-with-firebase-auth

Then use it in your components:

import * as React from 'react';
import * as firebase from 'firebase/app';
import 'firebase/auth';

import withFirebaseAuth, { WrappedComponentProps } from 'react-with-firebase-auth';

import firebaseConfig from './firebaseConfig';

const firebaseApp = firebase.initializeApp(firebaseConfig);

const firebaseAppAuth = firebaseApp.auth();

/** See the signature above to find out the available providers */
const providers = {
  googleProvider: new firebase.auth.GoogleAuthProvider(),
};

/** providers can be customised as per the Firebase documentation on auth providers **/
providers.googleProvider.setCustomParameters({
  hd: 'mycompany.com',
});

/** Create the FirebaseAuth component wrapper */
const createComponentWithAuth = withFirebaseAuth({
  providers,
  firebaseAppAuth,
});

const App = ({
  /** These props are provided by withFirebaseAuth HOC */
  signInWithEmailAndPassword,
  createUserWithEmailAndPassword,
  signInWithGoogle,
  signInWithFacebook,
  signInWithGithub,
  signInWithTwitter,
  signInAnonymously,
  signOut,
  setError,
  user,
  error,
  loading,
}: WrappedComponentProps) => (
  <React.Fragment>
    {
      user
        ? <h1>Hello, {user.displayName}</h1>
        : <h1>Log in</h1>
    }

    {
      user
        ? <button onClick={signOut}>Sign out</button>
        : <button onClick={signInWithGoogle}>Sign in with Google</button>
    }

    {
      loading && <h2>Loading..</h2>
    }
  </React.Fragment>
);

/** Wrap it */
export default createComponentWithAuth(App);

Examples

There are a few source code examples available:

You can also check a live demo example here:

Articles

Stargazers

Stargazers over time

License

MIT © Armando Magalhaes

More Repositories

1

react-firebase-authentication-medium

Resulting source code from "How to setup Firebase Authentication with React in 5 minutes (maybe 10)"
JavaScript
36
star
2

papercut

Papercut is a scraping/crawling library for Node.js built on top of JSDOM. It provides basic selector features together with features like Page Caching and Geosearch.
TypeScript
36
star
3

gringobot

Bot with utilities for the Telegram Group Programando na Gringa (https://go.d1m.dev/png)
TypeScript
34
star
4

react-query-filter

[WIP] A query filter builder component in React.
TypeScript
20
star
5

golinks

A URL shortener implementation in Next.js and GraphQL.
TypeScript
19
star
6

microservices

A microservices environment managed by Docker Compose.
16
star
7

philips-hue-experiments

Enabling interactions with Philips Hue API's through CLI using Node.js
JavaScript
8
star
8

ipcamera_bot

A bot that interfaces your IP Camera with Telegram. Supports multiple IP cameras. Written in Rust, made to be hosted in an old Android phone using Termux.
Rust
6
star
9

armand1m

hey you!
4
star
10

datastructures

my personal repo for data structure studies
TypeScript
4
star
11

create-formik-fields-comparator

Simple utility to help avoiding unnecessary re-renders in Formik forms
TypeScript
4
star
12

muminst-server

TypeScript
3
star
13

docker-logstash-sqlserver

Docker Automated Build for using Logstash with some SQL Server Binaries included.
Dockerfile
3
star
14

node-grumble

Mumble client library for Node.js, written in Typescript
TypeScript
3
star
15

muminst-client

Muminst Web Client
TypeScript
2
star
16

walking-cat

How to use React and HOCs to create a Controllable Walking 🐱
CSS
2
star
17

measu

(DEPRECATED) Measu is a project cost management tool built in React with Firebase
JavaScript
2
star
18

rodosol-telegram-bot

Telegram bot to fetch images from Terceira Ponte and Rodosol into your Telegram chat.
Rust
2
star
19

development-toolkit

🧰A set of plop generators for React components as a NPM package. (warning: very opinionated)
HTML
2
star
20

docker-logstash-oracle

Logstash docker image with Oracle Binaries prepared for use.
2
star
21

desmonta-retorno

Programa CLI que desmonta arquivos de retorno no modelo CNAB240 do Banco do Brasil
JavaScript
2
star
22

mumia-rs

Muminst Web Client built using Rust and Yew. Compiled to WASM.
Rust
1
star
23

bucket-service

This is a bucket microservice for serving and uploading static files.
JavaScript
1
star
24

muminst-server-rs

Muminst server written in Rust
Rust
1
star
25

microservices-compose-utils

microservices project compose bash utils
Shell
1
star
26

hello-service

Simple hello microservice for scalability studies.
JavaScript
1
star
27

haskell-insta-webscraper

very small html scraper in haskell
HTML
1
star
28

flexi-node-exam

Node.js exam project that Flexi required me to do when I entered the company in March 2017.
JavaScript
1
star
29

miditokeydaemon

miditokeydaemon is a configurable background process that can convert midi messages into keybindings and/or shell commands.
Rust
1
star
30

node-microservice-wrapper

Wrapper for a Node.js webserver implementation.
JavaScript
1
star