Skip to content

Commit 282b6bc

Browse files
committed
feat(ssr): allow resolving getCurrentUser
1 parent 2bf116c commit 282b6bc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/auth/user.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,33 @@ export function updateCurrentUserEmail(
8787

8888
// @internal
8989
type _UserState =
90-
// state 1 waiting for the initial load
90+
// state 1 waiting for the initial load: [Promise, resolveFn]
9191
| [Promise<_Nullable<User>>, (user: Ref<_Nullable<User>>) => void]
9292
// state 2 loaded
9393
| Ref<_Nullable<User>>
9494

95-
const initialUserMap = new WeakMap<FirebaseApp, _UserState>()
95+
/**
96+
* Map of user promises based on the firebase application. Used by `getCurrentUser()` to return a promise that resolves
97+
* the current user.
98+
* @internal
99+
*/
100+
export const initialUserMap = new WeakMap<FirebaseApp, _UserState>()
101+
102+
/**
103+
* Forcibly sets the initial user state. This is used by the server auth module to set the initial user state and make
104+
* `getCurrentUser()` work on the server during navigation and such.
105+
*
106+
* @internal
107+
*
108+
* @param firebaseApp - the firebase application
109+
* @param user - the user to set
110+
*/
111+
export function _setInitialUser(
112+
firebaseApp: FirebaseApp,
113+
user: Ref<_Nullable<User>>
114+
) {
115+
initialUserMap.set(firebaseApp, user)
116+
}
96117

97118
/**
98119
* @internal

src/server/auth.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FirebaseApp } from 'firebase/app'
22
import { User } from 'firebase/auth'
33
import { UserRecord } from 'firebase-admin/auth'
44
import { App, ref } from 'vue'
5-
import { authUserMap } from '../auth/user'
5+
import { authUserMap, _setInitialUser } from '../auth/user'
66
import { getGlobalScope } from '../globals'
77
import { _Nullable } from '../shared'
88

@@ -15,6 +15,7 @@ export function VueFireAuthServer(
1515
ref<_Nullable<User>>(createServerUser(userRecord))
1616
)!
1717
authUserMap.set(firebaseApp, user)
18+
_setInitialUser(firebaseApp, user)
1819
}
1920

2021
/**

0 commit comments

Comments
 (0)