1
1
import type { FirebaseApp } from 'firebase/app'
2
- import { getAuth , type User } from 'firebase/auth'
3
- import { App , ref } from 'vue-demi'
2
+ import {
3
+ type Dependencies as AuthDependencies ,
4
+ initializeAuth ,
5
+ type User ,
6
+ browserPopupRedirectResolver ,
7
+ browserLocalPersistence ,
8
+ browserSessionPersistence ,
9
+ indexedDBLocalPersistence ,
10
+ Auth ,
11
+ } from 'firebase/auth'
12
+ import { type App , ref , inject } from 'vue-demi'
4
13
import { useFirebaseApp } from '../app'
5
14
import { getGlobalScope } from '../globals'
6
15
import { isClient , _Nullable } from '../shared'
7
16
import { authUserMap , setupOnAuthStateChanged } from './user'
17
+ import { type VueFireModule } from '..'
8
18
9
19
export {
10
20
useCurrentUser ,
@@ -15,28 +25,80 @@ export {
15
25
} from './user'
16
26
17
27
/**
18
- * VueFire Auth Module to be added to the `VueFire` Vue plugin options.
28
+ * Options for VueFire Auth module.
29
+ */
30
+ export interface VueFireAuthOptions {
31
+ /**
32
+ * Initial value of the user. Used during SSR.
33
+ */
34
+ initialUser ?: _Nullable < User >
35
+
36
+ /**
37
+ * Options to pass to `initializeAuth()`.
38
+ */
39
+ dependencies : AuthDependencies
40
+ }
41
+
42
+ /**
43
+ * VueFire Auth Module to be added to the `VueFire` Vue plugin options. This calls the `VueFireAuthWithDependencies()`
44
+ * with **all** the dependencies, increasing bundle size. Consider using `VueFireAuthWithDependencies()` instead to
45
+ * better control the bundle size.
46
+ *
47
+ * @see https://firebase.google.com/docs/auth/web/custom-dependencies
19
48
*
20
49
* @example
21
50
*
22
- * ```ts
23
- * import { createApp } from 'vue'
24
- * import { VueFire, VueFireAuth } from 'vuefire'
51
+ *```ts
52
+ *import { createApp } from 'vue'
53
+ *import { VueFire, VueFireAuth } from 'vuefire'
54
+ *
55
+ *const app = createApp(App)
56
+ *app.use(VueFire, {
57
+ * modules: [VueFireAuth()],
58
+ *})
59
+ *```
60
+ *
61
+ * @param initialUser - initial value of the user. used for SSR
62
+ */
63
+ export function VueFireAuth ( initialUser ?: _Nullable < User > ) : VueFireModule {
64
+ return VueFireAuthWithDependencies ( {
65
+ initialUser,
66
+ dependencies : {
67
+ popupRedirectResolver : browserPopupRedirectResolver ,
68
+ persistence : [
69
+ indexedDBLocalPersistence ,
70
+ browserLocalPersistence ,
71
+ browserSessionPersistence ,
72
+ ] ,
73
+ } ,
74
+ } )
75
+ }
76
+
77
+ /**
78
+ * Key to be used to inject the auth instance into components. It allows avoiding to call `getAuth()`, which isn't tree
79
+ * shakable.
80
+ */
81
+ export const _VueFireAuthKey = Symbol ( 'VueFireAuth' )
82
+
83
+ /**
84
+ * VueFire Auth Module to be added to the `VueFire` Vue plugin options. It accepts dependencies to pass to
85
+ * `initializeAuth()` to better control the bundle size.
25
86
*
26
- * const app = createApp(App)
27
- * app.use(VueFire, {
28
- * modules: [VueFireAuth()],
29
- * })
30
- * ```
87
+ * @param options - user and options to pass to `initializeAuth()`.
31
88
*/
32
- export function VueFireAuth ( initialUser ?: _Nullable < User > ) {
89
+ export function VueFireAuthWithDependencies ( {
90
+ dependencies,
91
+ initialUser,
92
+ } : VueFireAuthOptions ) : VueFireModule {
33
93
return ( firebaseApp : FirebaseApp , app : App ) => {
34
94
const user = getGlobalScope ( firebaseApp , app ) . run ( ( ) =>
35
95
ref < _Nullable < User > > ( initialUser )
36
96
) !
37
97
// this should only be on client
38
98
authUserMap . set ( firebaseApp , user )
39
- setupOnAuthStateChanged ( user , firebaseApp )
99
+ const auth = initializeAuth ( firebaseApp , dependencies )
100
+ app . provide ( _VueFireAuthKey , auth )
101
+ setupOnAuthStateChanged ( user , auth )
40
102
}
41
103
}
42
104
@@ -47,6 +109,22 @@ export function VueFireAuth(initialUser?: _Nullable<User>) {
47
109
* @param name - name of the application
48
110
* @returns the Auth instance
49
111
*/
112
+ export function useFirebaseAuth ( ) : Auth | null
113
+ /**
114
+ * Retrieves the Firebase Auth instance. **Returns `null` on the server**. When using this function on the client in
115
+ * TypeScript, you can force the type with `useFirebaseAuth()!`.
116
+ *
117
+ * @deprecated - the name parameter is removed to enable tree shaking. If you have multiple applications, you **must**
118
+ * use "getAuth(firebaseApp)" or "getAuth(useFirebaseApp(name))" instead.`
119
+ *
120
+ * @param name - name of the application
121
+ * @returns the Auth instance
122
+ */
50
123
export function useFirebaseAuth ( name ?: string ) {
51
- return isClient ? getAuth ( useFirebaseApp ( name ) ) : null
124
+ if ( __DEV__ && name != null ) {
125
+ console . warn (
126
+ `[VueFire] useFirebaseAuth() no longer accepts a name parameter to enable tree shaking. If you have multiple applications, you must use "getAuth(firebaseApp)" or "getAuth(useFirebaseApp(name))" instead.`
127
+ )
128
+ }
129
+ return isClient ? inject ( _VueFireAuthKey ) : null
52
130
}
0 commit comments