Search code examples
javascriptfirebasefirebase-authenticationgoogle-signinfirebaseui

Firebase - SPA web application - issue with logout


I've recently made simple SPA application which connects to firebase using Google provider and loads data for authenticated user. Everything was great, until I tried to sign-out user using following method from documentation:

firebase.auth().signOut()

Logout was succesful, but after this, I can't sign-in again, because I'm receiving following error:

updateCurrentUser failed: First argument "user" must be an instance of Firebase User or null.

When I checked network tab in my browser, I've seen my user data in responses, so there Was an issue propably with the firebasewebui.

Things which I also tried

  • Sign-in in another browser - working
  • Sign-in in incognito mode - not working
  • Sign-in from other domain (for instance fake domain authorized in firebase console) - working
  • Wiped my entire Google Chrome profile from computer and add it again - not working
  • Sign-in from Android application - working (here there is no issue with sign-out and sign-in)

So it looks like it is something connected with domain and browser combination.

Here is my js code:

const firebase = require('firebase/app');
require('firebase/auth');
require('firebaseui');

const initializeFirebase = () => {
    const config = { /* config */ };

    firebase.initializeApp(config);

    firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
            // loads data
        } else {
            // visibility staff
            initializeFirebaseAuthForm();
        }
    });
}

const initializeFirebaseAuthForm = () => {
    const uiConfig = {
        callbacks: {
            signInSuccessWithAuthResult: function (authResult, redirectUrl) {
                return false;
            },
            uiShown: function () {
                visibilityManager(false);
            }
        },
        signInFlow: 'popup',
        signInOptions: [
            firebase.auth.GoogleAuthProvider.PROVIDER_ID
        ]
    };

    let ui = null;
    if (firebaseui.auth.AuthUI.getInstance()) {
        ui = firebaseui.auth.AuthUI.getInstance();
    } else {
        ui = new firebaseui.auth.AuthUI(firebase.auth());
    }
    ui.start('#firebaseui-auth-container', uiConfig);
}

document.addEventListener('DOMContentLoaded', function () {
    initializeFirebase();
});

In such case, my observer registered in onAuthStateChanged is not fired.


Solution

  • I've found answer by myself. There was couple of issues. First of all, Firebase initialization should be placed as 'global' like for example here: https://github.com/firebase/firebaseui-web/, especially this lines:

    firebase.initializeApp(config);
    ui = new firebaseui.auth.AuthUI(firebase.auth());
    

    Secondly, with my code from input, I've to get existing instance of ui using conditional. In firebase github, ui was created always using new operator and it was always created once per script run. I found out, that there is workaround - ui instance can be deleted using delete() promise.