Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let user specify the firebase app name #714

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/configTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Cookies from 'cookies'
import { PageURL } from './redirectTypes'

Check failure on line 2 in src/configTypes.ts

View workflow job for this annotation

GitHub Actions / build

Dependency cycle via ./createUser:3=>./config:6
import { User } from './createUser'
import { Spread } from './Spread'

Expand Down Expand Up @@ -90,6 +90,7 @@
// "G-MEASUREMENT_ID"
measurementId?: string
}
firebaseClientAppName?: string
tenantId?: string
cookies: Omit<Cookies.Option & Cookies.SetOption, 'sameSite'> & {
// The base name for the auth cookies.
Expand Down
4 changes: 3 additions & 1 deletion src/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { DecodedIdToken } from 'firebase-admin/auth'
import isClientSide from 'src/isClientSide'
import { Claims, filterStandardClaims } from 'src/claims'
import { getConfig } from './config'

Check failure on line 6 in src/createUser.ts

View workflow job for this annotation

GitHub Actions / build

Dependency cycle via ./configTypes:3

interface UserDeserialized {
id?: string
Expand Down Expand Up @@ -119,8 +120,9 @@
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)))
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/initFirebaseClientSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ 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(
'If not initializing the Firebase JS SDK elsewhere, you must provide "firebaseClientInitConfig" to next-firebase-auth.'
)
}

initializeApp(firebaseClientInitConfig)
initializeApp(firebaseClientInitConfig, firebaseClientAppName)
if (tenantId) {
getAuth().tenantId = tenantId
}
Expand All @@ -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}`
)
}
}
17 changes: 13 additions & 4 deletions src/useFirebaseUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}, [])

Expand Down
Loading