Skip to content

Commit d5d5e1b

Browse files
committed
feat(auth): allow directly passing the auth instance
See #1459
1 parent 8814646 commit d5d5e1b

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/auth/index.ts

+36-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
Auth,
1111
} from 'firebase/auth'
1212
import { type App, ref, inject } from 'vue-demi'
13-
import { useFirebaseApp } from '../app'
1413
import { getGlobalScope } from '../globals'
1514
import { isClient, _Nullable } from '../shared'
1615
import { authUserMap, setupOnAuthStateChanged } from './user'
@@ -39,6 +38,17 @@ export interface VueFireAuthOptions {
3938
dependencies: AuthDependencies
4039
}
4140

41+
/**
42+
* Options for VueFire Auth module when passing the auth instance directly.
43+
*/
44+
export interface VueFireAuthOptionsFromAuth
45+
extends Pick<VueFireAuthOptions, 'initialUser'> {
46+
/**
47+
* Auth instance to use.
48+
*/
49+
auth: Auth
50+
}
51+
4252
/**
4353
* VueFire Auth Module to be added to the `VueFire` Vue plugin options. This calls the `VueFireAuthWithDependencies()`
4454
* with **all** the dependencies, increasing bundle size. Consider using `VueFireAuthWithDependencies()` instead to
@@ -81,6 +91,29 @@ export function VueFireAuth(initialUser?: _Nullable<User>): VueFireModule {
8191
*/
8292
export const _VueFireAuthKey = Symbol('VueFireAuth')
8393

94+
/**
95+
* VueFire Auth Module to be added to the `VueFire` Vue plugin options. It accepts an auth instance rather than the
96+
* dependencies. It allows manually calling emulators and other advanced use cases. Prefer using
97+
* `VueFireAuthWithDependencies()` and `VueFireAuth()` for most use cases.
98+
*
99+
* @param options - auth instance and initial user
100+
*/
101+
export function VueFireAuthOptionsFromAuth({
102+
auth,
103+
initialUser,
104+
}: VueFireAuthOptionsFromAuth): VueFireModule {
105+
return (firebaseApp: FirebaseApp, app: App) => {
106+
const [user, _auth] = _VueFireAuthInit(
107+
firebaseApp,
108+
app,
109+
initialUser,
110+
undefined,
111+
auth
112+
)
113+
setupOnAuthStateChanged(user, _auth)
114+
}
115+
}
116+
84117
/**
85118
* VueFire Auth Module to be added to the `VueFire` Vue plugin options. It accepts dependencies to pass to
86119
* `initializeAuth()` to better control the bundle size.
@@ -110,14 +143,14 @@ export function _VueFireAuthInit(
110143
firebaseApp: FirebaseApp,
111144
app: App,
112145
initialUser: _Nullable<User>,
113-
dependencies: AuthDependencies
146+
dependencies?: AuthDependencies,
147+
auth = initializeAuth(firebaseApp, dependencies)
114148
) {
115149
const user = getGlobalScope(firebaseApp, app).run(() =>
116150
ref<_Nullable<User>>(initialUser)
117151
)!
118152
// TODO: Is it okay to have it both server and client?
119153
authUserMap.set(firebaseApp, user)
120-
const auth = initializeAuth(firebaseApp, dependencies)
121154
app.provide(_VueFireAuthKey, auth)
122155

123156
return [user, auth] as const

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export {
9090
type VueFireAuthOptions,
9191
VueFireAuth,
9292
VueFireAuthWithDependencies,
93+
VueFireAuthOptionsFromAuth,
9394
_VueFireAuthInit,
9495
useFirebaseAuth,
9596
_VueFireAuthKey,

0 commit comments

Comments
 (0)