Skip to content

Commit

Permalink
feat(ssr): allow resolving getCurrentUser
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 9, 2022
1 parent 2bf116c commit 282b6bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/auth/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,33 @@ export function updateCurrentUserEmail(

// @internal
type _UserState =
// state 1 waiting for the initial load
// state 1 waiting for the initial load: [Promise, resolveFn]
| [Promise<_Nullable<User>>, (user: Ref<_Nullable<User>>) => void]
// state 2 loaded
| Ref<_Nullable<User>>

const initialUserMap = new WeakMap<FirebaseApp, _UserState>()
/**
* Map of user promises based on the firebase application. Used by `getCurrentUser()` to return a promise that resolves
* the current user.
* @internal
*/
export const initialUserMap = new WeakMap<FirebaseApp, _UserState>()

/**
* Forcibly sets the initial user state. This is used by the server auth module to set the initial user state and make
* `getCurrentUser()` work on the server during navigation and such.
*
* @internal
*
* @param firebaseApp - the firebase application
* @param user - the user to set
*/
export function _setInitialUser(
firebaseApp: FirebaseApp,
user: Ref<_Nullable<User>>
) {
initialUserMap.set(firebaseApp, user)
}

/**
* @internal
Expand Down
3 changes: 2 additions & 1 deletion src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FirebaseApp } from 'firebase/app'
import { User } from 'firebase/auth'
import { UserRecord } from 'firebase-admin/auth'
import { App, ref } from 'vue'
import { authUserMap } from '../auth/user'
import { authUserMap, _setInitialUser } from '../auth/user'
import { getGlobalScope } from '../globals'
import { _Nullable } from '../shared'

Expand All @@ -15,6 +15,7 @@ export function VueFireAuthServer(
ref<_Nullable<User>>(createServerUser(userRecord))
)!
authUserMap.set(firebaseApp, user)
_setInitialUser(firebaseApp, user)
}

/**
Expand Down

0 comments on commit 282b6bc

Please sign in to comment.