There is an error when i try to login with any provider using firebase.
In browser, somehow firebase recovers from this error and im authenticated after 10 seconds. On a mobile device, this takes even longer..
I get the error:
api.js?onload=__iframefcb695643:29 Uncaught TypeError: u[v] is not a function
at Q.<computed> [as loaded_0] (api.js?onload=__iframefcb695643:29:145)
at cb=gapi.loaded_0?le=scs:1:6
I believe this error causes the whole authentication code to crash... for example the auth.onAuthStateChanged does not return a user, even though the collect call has succeeded (eg via google oauth https://identitytoolkit.googleapis.com/v1/accounts:lookup)
Could this be a bug in firebase? Or could it be a timing / import issue?
I have tried different versions of firebase, importing firebase in the index versus inline using npm package, but the problem stays the same..
any ideas would be most helpful!
im using firebase with firebaseui in compat mode in a reactjs SPA. "firebase": "^10.7.1", "firebaseui": "^6.1.0",
This is how i setup firebase:
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import * as firebaseui from 'firebaseui';
import { User, onAuthStateChanged, EmailAuthProvider, GoogleAuthProvider, OAuthProvider } from 'firebase/auth';
const firebaseConfig = {
...
};
const app = firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
auth.languageCode = 'nl';
export { app, auth };
then i use the following function in some react page (eg home.tsx) to load the ui:
const setupFirebaseAuthUI = () => {
const ui = firebaseui.auth.AuthUI.getInstance() || new firebaseui.auth.AuthUI(auth);
const providerApple = new OAuthProvider('apple.com');
const uiConfig = {
signInOptions: [EmailAuthProvider.PROVIDER_ID, GoogleAuthProvider.PROVIDER_ID, providerApple.providerId],
callbacks: {
signInSuccessWithAuthResult: authResult => {
navigate('/');
return false;
},
},
privacyPolicyUrl: 'https://example.nl/privacy',
tosUrl: 'https://example.nl/voorwaarden',
};
ui.start('#firebaseui-auth-container', uiConfig);
};
So i found something that may be the solution. Because of how react works, somehow the ui is loaded twice instead of once.
Only the first render of firebaseui will load the token from the redirect.. So when the second one starts, it has no token to receive anymore and it acts as a fresh resetted ui.
Solution is to set a ref when the first UI is loaded, so it is not allowed to load the ui twice in the login component.
I've setup an example with react create app and this is working code:
LoginPage.tsx
you can change the implementation of
signInSuccessWithAuthResultand/oronAuthStateChangedaccordingly.firebaseConfig.tsx