Installation and Usage
For an example on how to use the FirebaseAuth react component have a look at the example folder.
Install the npm package in your React app:
npm install --save react-firebaseui
You also need the firebase
package installed which is a peer dependency:
npm install --save firebase
In your app:
- Import the
FirebaseAuth
or theStyledFirebaseAuth
component fromreact-firebaseui
and importfirebase
. - Configure Firebase as described in the Firebase Docs.
- Write a Firebase UI configuration as described in firebase/firebaseui-web.
- Use the
FirebaseAuth
component in your template passing it the Firebase UI configuration and a Firebase Auth instance.
FirebaseAuth
vs. StyledFirebaseAuth
There are two similar components that allow you to add FirebaseUI auth to your application: FirebaseAuth
and StyledFirebaseAuth
.
FirebaseAuth
has a reference to the FirebaseUI CSS file (itrequires
the CSS).StyledFirebaseAuth
is bundled with the CSS directly.
For simplicity you should use StyledFirebaseAuth
and for potential better performances and build sizes you can use FirebaseAuth
. FirebaseAuth
is meant to be used with a CSS/style loader as part of your webpack built configuration. See the Packing your app section.
Using StyledFirebaseAuth
with a redirect
Below is an example on how to use FirebaseAuth
with a redirect upon sign-in:
// Import FirebaseAuth and firebase.
import React from 'react';
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth';
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
// Configure Firebase.
const config = {
apiKey: 'AIzaSyAeue-AsYu76MMQlTOM-KlbYBlusW9c1FM',
authDomain: 'myproject-1234.firebaseapp.com',
// ...
};
firebase.initializeApp(config);
// Configure FirebaseUI.
const uiConfig = {
// Popup signin flow rather than redirect flow.
signInFlow: 'popup',
// Redirect to /signedIn after sign in is successful. Alternatively you can provide a callbacks.signInSuccess function.
signInSuccessUrl: '/signedIn',
// We will display Google and Facebook as auth providers.
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
firebase.auth.FacebookAuthProvider.PROVIDER_ID,
],
};
function SignInScreen() {
return (
<div>
<h1>My App</h1>
<p>Please sign-in:</p>
<StyledFirebaseAuth uiConfig={uiConfig} firebaseAuth={firebase.auth()} />
</div>
);
}
export default SignInScreen
Using StyledFirebaseAuth
with local state.
Below is an example on how to use StyledFirebaseAuth
with a state change upon sign-in:
// Import FirebaseAuth and firebase.
import React, { useEffect, useState } from 'react';
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth';
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
// Configure Firebase.
const config = {
apiKey: 'AIzaSyAeue-AsYu76MMQlTOM-KlbYBlusW9c1FM',
authDomain: 'myproject-1234.firebaseapp.com',
// ...
};
firebase.initializeApp(config);
// Configure FirebaseUI.
const uiConfig = {
// Popup signin flow rather than redirect flow.
signInFlow: 'popup',
// We will display Google and Facebook as auth providers.
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
firebase.auth.FacebookAuthProvider.PROVIDER_ID
],
callbacks: {
// Avoid redirects after sign-in.
signInSuccessWithAuthResult: () => false,
},
};
function SignInScreen() {
const [isSignedIn, setIsSignedIn] = useState(false); // Local signed-in state.
// Listen to the Firebase Auth state and set the local state.
useEffect(() => {
const unregisterAuthObserver = firebase.auth().onAuthStateChanged(user => {
setIsSignedIn(!!user);
});
return () => unregisterAuthObserver(); // Make sure we un-register Firebase observers when the component unmounts.
}, []);
if (!isSignedIn) {
return (
<div>
<h1>My App</h1>
<p>Please sign-in:</p>
<StyledFirebaseAuth uiConfig={uiConfig} firebaseAuth={firebase.auth()} />
</div>
);
}
return (
<div>
<h1>My App</h1>
<p>Welcome {firebase.auth().currentUser.displayName}! You are now signed-in!</p>
<a onClick={() => firebase.auth().signOut()}>Sign-out</a>
</div>
);
}
export default SignInScreen;
Accessing the FirebaseUI instance
To allow for further configuration you can access the firebaseUI instance before it is started.
To do this you can pass a uiCallback
callback function that wil be passed the Firebase UI instance. For example here is how to enable the disableAutoSignIn()
option:
// ...
return (
<div>
<h1>My App</h1>
<p>Please sign-in:</p>
<StyledFirebaseAuth uiCallback={ui => ui.disableAutoSignIn()} uiConfig={uiConfig} firebaseAuth={firebase.auth()}/>
</div>
);