Skip to content

A demo which shows how to implement the firebase authentication mechanism in react

Notifications You must be signed in to change notification settings

innFactory/react-firebase-auth-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Firebase Authenticator

Needs MaterialUI with theme, firebase and react.

  1. Install the firebase dependency via npm (tested with firebase 5.3.0)
npm install firebase --save
  1. Set Callback Method

The callback for the auth state change must be initialized as high as possible in the component hirarchy. For example in the App.tsx

import Firebase from './FirebaseApp';
...
componentWillMount() {
        // Callback for Auth State Changes
        if (Firebase.auth) {
            Firebase.auth().onAuthStateChanged((user: any) => {
                if (user) {
                    // User Login Actions
                } else {
                   // User Logout Actions
                }
            });
        }

    }
  1. Configure FirebaseApp.tsx
const config = {
    apiKey: 'CONFIG.FIREBASE.APIKEY',
    authDomain: 'CONFIG.FIREBASE.AUTHDOMAIN',
    databaseURL: 'CONFIG.FIREBASE.DATABASEURL',
    projectId: 'CONFIG.FIREBASE.PROJECTID',
    storageBucket: 'CONFIG.FIREBASE.STORAGEBUCKET',
    messagingSenderId: 'CONFIG.FIREBASE.MESSAGINGSENDERID'
};
  1. Copy the firebase.d.ts typings into the main "typings" folder

  2. Create authenticator

window.open('Url or endpoit', _blank) is called for the urls

    <Authenticator
        onSuccess={() => /* Action after Success */ }
        privacyPolicyUrl={/* Url or endpoint to the privacyPolicy */ }
        siteNoticeUrl={/* Url or endpoint to the siteNotice */ }
    />
  1. Control all paths to the imports