Skip to content

Commit 840ca8b

Browse files
committed
feat(nuxt): split plugins and check options ot add them
1 parent 20ab13d commit 840ca8b

File tree

4 files changed

+66
-37
lines changed

4 files changed

+66
-37
lines changed

packages/nuxt/src/module.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
ServiceAccount,
1616
App as FirebaseAdminApp,
1717
} from 'firebase-admin/app'
18+
import { markRaw } from 'vue'
1819
import type { NuxtVueFireAppCheckOptions } from './runtime/app-check'
1920

2021
export interface VueFireNuxtModuleOptions {
@@ -94,9 +95,10 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
9495
new URL('../templates', import.meta.url)
9596
)
9697

98+
// TODO: I don't think the appConfig is the right place to store these as it makes things reactive
9799
// Let plugins and the user access the firebase config within the app
98-
nuxt.options.appConfig.firebaseConfig = options.config
99-
nuxt.options.appConfig.vuefireOptions = options
100+
nuxt.options.appConfig.firebaseConfig = markRaw(options.config)
101+
nuxt.options.appConfig.vuefireOptions = markRaw(options)
100102

101103
// nuxt.options.build.transpile.push(templatesDir)
102104
nuxt.options.build.transpile.push(runtimeDir)
@@ -114,7 +116,7 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
114116
'[VueFire]: Missing firebase "admin" config. Provide an "admin" option to the VueFire module options. This is necessary to use the auth or app-check module.'
115117
)
116118
}
117-
nuxt.options.appConfig.firebaseAdmin = options.admin
119+
nuxt.options.appConfig.firebaseAdmin = markRaw(options.admin)
118120
}
119121
}
120122

@@ -127,11 +129,24 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
127129

128130
// this allows us to the order of the plugins
129131
nuxt.hook('modules:done', () => {
130-
addPlugin(resolve(runtimeDir, 'auth/plugin.client'))
131-
// must be added after the admin module to use the admin app
132-
addPlugin(resolve(runtimeDir, 'auth/plugin.server'))
132+
if (options.auth) {
133+
addPlugin(resolve(runtimeDir, 'auth/plugin.client'))
134+
// must be added after the admin module to use the admin app
135+
addPlugin(resolve(runtimeDir, 'auth/plugin.server'))
136+
}
133137

134-
addPlugin(resolve(runtimeDir, 'admin/plugin.server'))
138+
if (options.admin) {
139+
if (!nuxt.options.ssr) {
140+
console.warn(
141+
'[VueFire]: The "admin" option is only used during SSR. You should reenable ssr to use it.'
142+
)
143+
}
144+
addPlugin(resolve(runtimeDir, 'admin/plugin.server'))
145+
}
146+
147+
if (options.appCheck) {
148+
addPlugin(resolve(runtimeDir, 'app-check/plugin'))
149+
}
135150

136151
// plugin are added in reverse order
137152
addPluginTemplate({
@@ -142,6 +157,8 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
142157
ssr: nuxt.options.ssr,
143158
},
144159
})
160+
161+
// adds the firebase app to each application
145162
addPlugin(resolve(runtimeDir, 'app/plugin'))
146163
})
147164
},
@@ -158,6 +175,7 @@ declare module '@nuxt/schema' {
158175
export interface AppConfig {
159176
/**
160177
* Firebase config to initialize the app.
178+
* @internal
161179
*/
162180
firebaseConfig: FirebaseOptions
163181

@@ -169,6 +187,7 @@ declare module '@nuxt/schema' {
169187

170188
/**
171189
* Firebase Admin options passed to VueFire module. Only available on the server.
190+
* @internal
172191
*/
173192
firebaseAdmin?: {
174193
config: Omit<AppOptions, 'credential'>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { FirebaseApp } from 'firebase/app'
2+
import {
3+
ReCaptchaV3Provider,
4+
ReCaptchaEnterpriseProvider,
5+
CustomProvider,
6+
AppCheckOptions,
7+
} from 'firebase/app-check'
8+
import { VueFireAppCheck } from 'vuefire'
9+
import { defineNuxtPlugin, useAppConfig } from '#app'
10+
11+
/**
12+
* Plugin to initialize the appCheck module.
13+
*/
14+
export default defineNuxtPlugin((nuxtApp) => {
15+
const appConfig = useAppConfig()
16+
// NOTE: appCheck is present because of the check in module.ts
17+
const options = appConfig.vuefireOptions.appCheck!
18+
const firebaseApp = nuxtApp.$firebaseApp as FirebaseApp
19+
20+
// default provider for server
21+
let provider: AppCheckOptions['provider'] = new CustomProvider({
22+
getToken: () =>
23+
Promise.reject(
24+
process.env.NODE_ENV !== 'production'
25+
? new Error("[VueFire]: This shouldn't be called on server.")
26+
: new Error('app-check/invalid-provider')
27+
),
28+
})
29+
30+
if (options.provider === 'ReCaptchaV3') {
31+
provider = new ReCaptchaV3Provider(options.key)
32+
} else if (options.provider === 'ReCaptchaEnterprise') {
33+
provider = new ReCaptchaEnterpriseProvider(options.key)
34+
}
35+
36+
VueFireAppCheck({
37+
...options,
38+
provider,
39+
})(firebaseApp, nuxtApp.vueApp)
40+
})

packages/nuxt/src/runtime/app/plugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { defineNuxtPlugin, useAppConfig } from '#app'
44
/**
55
* Initializes the app and provides it to others.
66
*/
7-
87
export default defineNuxtPlugin(() => {
98
const appConfig = useAppConfig()
109

packages/nuxt/templates/plugin.ejs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import {
2-
usePendingPromises,
32
VueFire,
4-
53
useSSRInitialState,
6-
7-
// TODO: conditional imports
8-
VueFireAppCheck,
94
} from 'vuefire'
10-
import { initializeApp } from 'firebase/app'
115
import {
126
defineNuxtPlugin,
137
useAppConfig,
14-
useRuntimeConfig,
158
} from '#app'
169
import {
1710
connectFirestoreEmulator,
@@ -40,30 +33,8 @@ export default defineNuxtPlugin((nuxtApp) => {
4033
// connectFirestoreEmulator(getFirestore(firebaseApp), 'localhost', 8080)
4134
// connectDatabaseEmulator(getDatabase(firebaseApp), 'localhost', 8081)
4235

43-
const modules = []
44-
45-
<% if(options.auth) { %>
46-
// modules.push(VueFireAuth())
47-
<% } %>
48-
49-
<% if(options.appCheck) { %>
50-
modules.push(VueFireAppCheck({
51-
...appConfig.vuefireOptions.appCheck,
52-
provider: '<%= options.appCheck.provider %>' === 'ReCaptchaV3'
53-
? new ReCaptchaV3Provider('<%= options.appCheck.key %>')
54-
: new CustomProvider({
55-
getToken: () => Promise.reject(
56-
process.env.NODE_ENV !== 'production'
57-
? new Error('Unknown provider "<%= options.appCheck.provider %>"')
58-
: new Error()
59-
),
60-
}),
61-
}))
62-
<% } %>
63-
6436
nuxtApp.vueApp.use(VueFire, {
6537
firebaseApp,
66-
modules,
6738
})
6839

6940
<% if(options.ssr) { %>

0 commit comments

Comments
 (0)