diff --git a/src/configTypes.ts b/src/configTypes.ts index f36a3307..a14a0130 100644 --- a/src/configTypes.ts +++ b/src/configTypes.ts @@ -90,6 +90,7 @@ export interface ConfigInput { // "G-MEASUREMENT_ID" measurementId?: string } + firebaseClientAppName?: string tenantId?: string cookies: Omit & { // The base name for the auth cookies. diff --git a/src/createUser.ts b/src/createUser.ts index 4e57d700..16eaf240 100644 --- a/src/createUser.ts +++ b/src/createUser.ts @@ -3,6 +3,7 @@ import { User as FirebaseUser } from 'firebase/auth' import { DecodedIdToken } from 'firebase-admin/auth' import isClientSide from 'src/isClientSide' import { Claims, filterStandardClaims } from 'src/claims' +import { getConfig } from './config' interface UserDeserialized { id?: string @@ -119,8 +120,9 @@ const createUser = ({ const { getApp } = require('firebase/app') // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires const { getAuth, signOut } = require('firebase/auth') + const { firebaseClientAppName } = getConfig() - signOutFunc = async () => signOut(getAuth(getApp())) + signOutFunc = async () => signOut(getAuth(getApp(firebaseClientAppName))) } /** diff --git a/src/initFirebaseClientSDK.ts b/src/initFirebaseClientSDK.ts index 22dc7372..96c09215 100644 --- a/src/initFirebaseClientSDK.ts +++ b/src/initFirebaseClientSDK.ts @@ -4,8 +4,12 @@ import { getConfig } from 'src/config' import logDebug from 'src/logDebug' export default function initFirebaseClientSDK() { - const { firebaseClientInitConfig, firebaseAuthEmulatorHost, tenantId } = - getConfig() + const { + firebaseClientInitConfig, + firebaseAuthEmulatorHost, + tenantId, + firebaseClientAppName, + } = getConfig() if (!getApps().length) { if (!firebaseClientInitConfig) { throw new Error( @@ -13,7 +17,7 @@ export default function initFirebaseClientSDK() { ) } - initializeApp(firebaseClientInitConfig) + initializeApp(firebaseClientInitConfig, firebaseClientAppName) if (tenantId) { getAuth().tenantId = tenantId } @@ -25,6 +29,9 @@ export default function initFirebaseClientSDK() { } // If the user has provided the firebaseAuthEmulatorHost address, set the emulator if (firebaseAuthEmulatorHost) { - connectAuthEmulator(getAuth(getApp()), `http://${firebaseAuthEmulatorHost}`) + connectAuthEmulator( + getAuth(getApp(firebaseClientAppName)), + `http://${firebaseAuthEmulatorHost}` + ) } } diff --git a/src/useFirebaseUser.ts b/src/useFirebaseUser.ts index e5bc20f5..f655633f 100644 --- a/src/useFirebaseUser.ts +++ b/src/useFirebaseUser.ts @@ -175,10 +175,19 @@ const useFirebaseUser = () => { } // https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onidtokenchanged - const unsubscribe = onIdTokenChanged(getAuth(getApp()), onIdTokenChange) - return () => { - unsubscribe() - isCancelled = true + try { + const { firebaseClientAppName } = getConfig() + logDebug(`[withUser] firebaseClientAppName |${firebaseClientAppName}|`) + const app = getApp(firebaseClientAppName) + logDebug('[withUser] App loaded', app) + const unsubscribe = onIdTokenChanged(getAuth(app), onIdTokenChange) + return () => { + unsubscribe() + isCancelled = true + } + } catch (error) { + logDebug('[withUser] init error', error) + throw error } }, [])