Skip to content

Commit

Permalink
feat(auth): expose internal utils for SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 1, 2023
1 parent a3c64a2 commit 486b415
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
35 changes: 28 additions & 7 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export function VueFireAuth(initialUser?: _Nullable<User>): VueFireModule {
/**
* Key to be used to inject the auth instance into components. It allows avoiding to call `getAuth()`, which isn't tree
* shakable.
* @internal
*/
export const _VueFireAuthKey = Symbol('VueFireAuth')

Expand All @@ -91,17 +92,37 @@ export function VueFireAuthWithDependencies({
initialUser,
}: VueFireAuthOptions): VueFireModule {
return (firebaseApp: FirebaseApp, app: App) => {
const user = getGlobalScope(firebaseApp, app).run(() =>
ref<_Nullable<User>>(initialUser)
)!
// this should only be on client
authUserMap.set(firebaseApp, user)
const auth = initializeAuth(firebaseApp, dependencies)
app.provide(_VueFireAuthKey, auth)
const [user, auth] = _VueFireAuthInit(
firebaseApp,
app,
initialUser,
dependencies
)
setupOnAuthStateChanged(user, auth)
}
}

/**
* initializes auth for both the server and client.
* @internal
*/
export function _VueFireAuthInit(
firebaseApp: FirebaseApp,
app: App,
initialUser: _Nullable<User>,
dependencies: AuthDependencies
) {
const user = getGlobalScope(firebaseApp, app).run(() =>
ref<_Nullable<User>>(initialUser)
)!
// TODO: Is it okay to have it both server and client?
authUserMap.set(firebaseApp, user)
const auth = initializeAuth(firebaseApp, dependencies)
app.provide(_VueFireAuthKey, auth)

return [user, auth] as const
}

/**
* Retrieves the Firebase Auth instance. **Returns `null` on the server**. When using this function on the client in
* TypeScript, you can force the type with `useFirebaseAuth()!`.
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export {
type VueFireAuthOptions,
VueFireAuth,
VueFireAuthWithDependencies,
_VueFireAuthInit,
useFirebaseAuth,
_VueFireAuthKey,
getCurrentUser,
updateCurrentUserProfile,
} from './auth'
Expand Down
7 changes: 4 additions & 3 deletions src/server/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FirebaseApp } from 'firebase/app'
import type { User } from 'firebase/auth'
import { type User } from 'firebase/auth'
import type { DecodedIdToken, UserRecord } from 'firebase-admin/auth'
import { App, ref } from 'vue'
import { authUserMap, _setInitialUser } from '../auth/user'
Expand All @@ -9,6 +9,7 @@ import type { App as AdminApp } from 'firebase-admin/app'
import { getAuth as getAdminAuth } from 'firebase-admin/auth'
import { logger } from './logging'
import { isFirebaseError } from './utils'
import { _VueFireAuthKey } from '../auth'

// MUST be named `__session` to be kept in Firebase context, therefore this name is hardcoded
// https://firebase.google.com/docs/hosting/manage-cache#using_cookies
Expand All @@ -21,10 +22,10 @@ export const AUTH_COOKIE_NAME = '__session'
export function VueFireAuthServer(
firebaseApp: FirebaseApp,
app: App<unknown>,
userRecord: _Nullable<User>
initialUser: _Nullable<User>
) {
const user = getGlobalScope(firebaseApp, app).run(() =>
ref<_Nullable<User>>(userRecord)
ref<_Nullable<User>>(initialUser)
)!
authUserMap.set(firebaseApp, user)
_setInitialUser(firebaseApp, user)
Expand Down

0 comments on commit 486b415

Please sign in to comment.