Skip to content

Commit b9c7599

Browse files
author
Nicolas Garnier
committed
Now deleting the firebaseUi instance instead of just reset-ing it when the component is unmounted.
1 parent d18f77c commit b9c7599

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/FirebaseAuth.jsx

+26-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import React from 'react';
2020
// Global ID for the element.
2121
const ELEMENT_ID = 'firebaseui_container';
2222

23+
// Promise that resolves unless the FirebaseUI instance is currently being deleted.
24+
let firebaseUiDeletion = Promise.resolve();
25+
2326
/**
2427
* React Component wrapper for the FirebaseUI Auth widget.
2528
*/
@@ -46,24 +49,36 @@ export default class FirebaseAuth extends React.Component {
4649
// Import the css only on the client.
4750
require('firebaseui/dist/firebaseui.css');
4851

49-
// Firebase UI only works on the Client. So we're loading in `componentDidMount`.
52+
// Firebase UI only works on the Client. So we're loading the package in `componentDidMount`
53+
// So that this works when doing server-side rendering.
5054
const firebaseui = require('firebaseui');
51-
this.firebaseUiWidget = firebaseui.auth.AuthUI.getInstance()
52-
|| new firebaseui.auth.AuthUI(this.firebaseAuth);
53-
if (this.uiConfig.signInFlow === 'popup') {
54-
this.firebaseUiWidget.reset();
55-
}
56-
if (this.uiCallback) {
57-
this.uiCallback(this.firebaseUiWidget);
58-
}
59-
this.firebaseUiWidget.start('#' + ELEMENT_ID, this.uiConfig);
55+
56+
// Wait in case the firebase UI instance is being deleted.
57+
// This can happen if you unmount/remount the element quickly.
58+
firebaseUiDeletion.then(() => {
59+
// Get or Create a firebaseUI instance.
60+
this.firebaseUiWidget = firebaseui.auth.AuthUI.getInstance()
61+
|| new firebaseui.auth.AuthUI(this.firebaseAuth);
62+
if (this.uiConfig.signInFlow === 'popup') {
63+
this.firebaseUiWidget.reset();
64+
}
65+
66+
// Trigger the callback if any was set.
67+
if (this.uiCallback) {
68+
this.uiCallback(this.firebaseUiWidget);
69+
}
70+
71+
// Render the firebaseUi Widget.
72+
this.firebaseUiWidget.start('#' + ELEMENT_ID, this.uiConfig);
73+
});
6074
}
6175

6276
/**
6377
* @inheritDoc
6478
*/
6579
componentWillUnmount() {
66-
this.firebaseUiWidget.reset();
80+
this.unregisterAuthObserver();
81+
firebaseUiDeletion = this.firebaseUiWidget.delete();
6782
}
6883

6984
/**

0 commit comments

Comments
 (0)