firebase / firebaseui-web

FirebaseUI is an open-source JavaScript library for Web that provides simple, customizable UI bindings on top of Firebase SDKs to eliminate boilerplate code and promote best practices.
https://firebase.google.com/
Apache License 2.0
4.58k stars 1.06k forks source link

user.providerData is empty after update to v6 #917

Closed henrik-d closed 2 years ago

henrik-d commented 2 years ago

[REQUIRED] Describe your environment

[REQUIRED] Describe the problem

Since updating to firebaseui 6.0.0, after signing up or signing in the currentUser.providerData is empty. This problem did not occur in v4 and v5.

Relevant Code:

providerData is empty when using firebaseui for signIn:

import firebase from 'firebase/compat/app'
import 'firebase/compat/auth';
import * as firebaseui from 'firebaseui'
import 'firebaseui/dist/firebaseui.css'

const firebaseConfig = {...};
const app = firebase.initializeApp(firebaseConfig);
const auth = firebase.auth(app);

auth.onAuthStateChanged(user => {
    // Problem: authResult.user.providerData is empty after signin with firebaseui, ❌
    // but has one element when using auth.signInWithEmailAndPassword() directly ✅
    console.log("user signed in", user);
})

const signInSuccessWithAuthResult = (authResult: any, redirectUrl?: string) => {
    console.log("authResult", authResult); // Problem: authResult.user.providerData is empty! ❌
    auth.signOut();
    return false;
}

const ui = new firebaseui.auth.AuthUI(auth);
ui.start('#root', {
    signInOptions: [
        firebase.auth.EmailAuthProvider.PROVIDER_ID
    ],
    callbacks: {
        signInSuccessWithAuthResult,
    },
});

When using firebase directly, it works as expected:

auth.signInWithEmailAndPassword("my-email@email.de", "password").then(credential => {
    console.log("signInWithEmailAndPassword", credential); // credential.user.providerData.length === 1 ✅
    return auth.signOut();
})
boldtrn commented 2 years ago

I can verify this, we are seeing the same issue.

When reloading the page, the providerData appears.

boldtrn commented 2 years ago

As an intermediary fix, you can do the following:

if (fbUser.providerData && fbUser.providerData.length === 0) {
            console.debug('User data is not fully loaded, initiate reload');
            fbUser.reload().then(() => // update your UI here...);
}
sam-gc commented 2 years ago

Thanks for the report folks; the linked PR will fix the issue. You'll need to update your version of the Firebase JS SDK (i.e. not Firebase UI) to benefit from the fix once the PR is merged/released