Skip to content

Commit 7a353ab

Browse files
committed
refactor(auth): use the firebase app as key
1 parent 094f6a5 commit 7a353ab

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/auth/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { App, ref, shallowRef } from 'vue-demi'
44
import { useFirebaseApp } from '../app'
55
import { getGlobalScope } from '../globals'
66
import { _Nullable } from '../shared'
7-
import { AuthUserInjectSymbol, setupOnAuthStateChanged } from './user'
7+
import { authUserMap, setupOnAuthStateChanged } from './user'
88

99
export {
1010
useCurrentUser,
@@ -44,8 +44,7 @@ modules: [VueFireAuth()]`)
4444
const user = getGlobalScope(firebaseApp, app).run(() =>
4545
ref<_Nullable<User>>()
4646
)!
47-
// userMap.set(app, user)
48-
app.provide(AuthUserInjectSymbol, user)
47+
authUserMap.set(firebaseApp, user)
4948
setupOnAuthStateChanged(user, firebaseApp)
5049
}
5150
}

src/auth/user.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,28 @@ import { inject, InjectionKey, Ref } from 'vue-demi'
1212
import { useFirebaseApp } from '../app'
1313
import type { _MaybeRef, _Nullable } from '../shared'
1414

15-
export const AuthUserInjectSymbol: InjectionKey<Ref<_Nullable<User>>> =
16-
Symbol('user')
15+
/**
16+
* Maps an application to a user
17+
* @internal
18+
*/
19+
export const authUserMap = new WeakMap<FirebaseApp, Ref<_Nullable<User>>>()
1720

1821
/**
1922
* Returns a shallowRef of the currently authenticated user in the firebase app. The ref is null if no user is
2023
* authenticated or when the user logs out. The ref is undefined when the user is not yet loaded.
24+
* @param name - name of the application
2125
*/
22-
export function useCurrentUser() {
23-
// TODO: warn no current instance in DEV
24-
return inject(AuthUserInjectSymbol)!
26+
export function useCurrentUser(name?: string) {
27+
// TODO: write a test
28+
if (
29+
process.env.NODE_ENV !== 'production' &&
30+
!authUserMap.has(useFirebaseApp(name))
31+
) {
32+
throw new Error(
33+
`[VueFire] useCurrentUser() called before the VueFireAuth module was added to the VueFire plugin. This will fail in production.`
34+
)
35+
}
36+
return authUserMap.get(useFirebaseApp())!
2537
}
2638

2739
/**

0 commit comments

Comments
 (0)