@@ -133,20 +133,45 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
133133 )
134134 }
135135
136- // Add the session handler than mints a cookie for the user
137- if ( nuxt . options . ssr && hasServiceAccount ) {
136+ if ( options . appCheck ) {
137+ addPlugin ( resolve ( runtimeDir , 'app-check/plugin.client' ) )
138+ // TODO: ensure this is the only necessary check. Maybe we need to check if server
139+ if ( hasServiceAccount ) {
140+ addPlugin ( resolve ( runtimeDir , 'app-check/plugin.server' ) )
141+ } else if ( nuxt . options . ssr ) {
142+ log (
143+ 'warn' ,
144+ 'You activated both SSR and app-check but you are not providing an admin config. If you render or prerender any page using app-check, you will get an error. In that case, provide an admin config to the nuxt-vuefire module.'
145+ // TODO: link about how to provide admin credentials
146+ )
147+ }
148+ }
149+
150+ // this adds the VueFire plugin and handle SSR state serialization and hydration
151+ addPluginTemplate ( {
152+ src : normalize ( resolve ( templatesDir , 'plugin.ejs' ) ) ,
153+
154+ options : {
155+ ...options ,
156+ ssr : nuxt . options . ssr ,
157+ } ,
158+ } )
159+
160+ // if (nuxt.options.ssr && hasServiceAccount) {
161+ if ( options . auth ) {
162+ // Add the session handler than mints a cookie for the user
138163 addServerHandler ( {
139164 route : '/api/__session' ,
140- // handler: resolve(runtimeDir, './auth/api.session'),
141165 handler : resolve ( runtimeDir , './auth/api.session-verification' ) ,
142166 } )
143167
144168 // must be added after (which means before in code) the plugin module
145169 addPlugin ( resolve ( runtimeDir , 'auth/plugin-mint-cookie.client' ) )
146170 }
147171
172+ // hydrates the user if any
148173 addPlugin ( resolve ( runtimeDir , 'auth/plugin.client' ) )
149- // must be added after the admin module to use the admin app
174+ // loads the user on the current app
150175 addPlugin ( resolve ( runtimeDir , 'auth/plugin.server' ) )
151176
152177 addVueFireImports ( [
@@ -163,30 +188,6 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
163188 ] )
164189 }
165190
166- if ( options . appCheck ) {
167- addPlugin ( resolve ( runtimeDir , 'app-check/plugin.client' ) )
168- // TODO: ensure this is the only necessary check. Maybe we need to check if server
169- if ( hasServiceAccount ) {
170- addPlugin ( resolve ( runtimeDir , 'app-check/plugin.server' ) )
171- } else if ( nuxt . options . ssr ) {
172- log (
173- 'warn' ,
174- 'You activated both SSR and app-check but you are not providing an admin config. If you render or prerender any page using app-check, you will get an error. In that case, provide an admin config to the nuxt-vuefire module.'
175- // TODO: link about how to provide admin credentials
176- )
177- }
178- }
179-
180- // this adds the VueFire plugin and handle SSR state serialization and hydration
181- addPluginTemplate ( {
182- src : normalize ( resolve ( templatesDir , 'plugin.ejs' ) ) ,
183-
184- options : {
185- ...options ,
186- ssr : nuxt . options . ssr ,
187- } ,
188- } )
189-
190191 // adds the firebase app to each application
191192 addPlugin ( resolve ( runtimeDir , 'app/plugin.client' ) )
192193 addPlugin ( resolve ( runtimeDir , 'app/plugin.server' ) )
@@ -200,16 +201,18 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
200201 )
201202 }
202203
203- // TODO: remove this runtime config if it's not needed as it could include sensitive data
204- if ( options . admin ) {
205- nuxt . options . appConfig . firebaseAdmin = markRaw ( options . admin )
206- }
207-
204+ // TODO: add with ssr
208205 if ( hasServiceAccount ) {
209- // this plugin adds the user so it's accessible directly in the app as well
210206 if ( options . auth ) {
211- addPlugin ( resolve ( runtimeDir , 'admin/plugin-auth-user.server' ) )
207+ // decodes user token from cookie if any
208+ addPlugin ( resolve ( runtimeDir , 'auth/plugin-user-token.server' ) )
209+ }
210+
211+ if ( options . admin ?. options ) {
212+ // used by the admin app plugin to initialize the admin app
213+ nuxt . options . runtimeConfig . vuefireAdminOptions = options . admin . options
212214 }
215+ // injects firebaseAdminApp
213216 addPlugin ( resolve ( runtimeDir , 'admin/plugin.server' ) )
214217 }
215218 }
@@ -284,26 +287,32 @@ export type {
284287 NuxtVueFireAppCheckOptionsReCaptchaEnterprise ,
285288} from './runtime/app-check'
286289
287- declare module '@nuxt/schema' {
288- export interface AppConfig {
289- /**
290- * Firebase config to initialize the app.
291- * @internal
292- */
293- firebaseConfig : FirebaseOptions
290+ interface VueFireRuntimeConfig {
291+ /**
292+ * Firebase Admin options passed to VueFire module. Only available on the server.
293+ *
294+ * @internal
295+ */
296+ vuefireAdminOptions ?: Omit < AppOptions , 'credential' >
297+ }
294298
295- /**
296- * VueFireNuxt options used within plugins.
297- * @internal
298- */
299- vuefireOptions : Pick < VueFireNuxtModuleOptions , 'appCheck' | 'auth' >
299+ interface VueFireAppConfig {
300+ /**
301+ * Firebase config to initialize the app.
302+ * @internal
303+ */
304+ firebaseConfig : FirebaseOptions
300305
301- /**
302- * Firebase Admin options passed to VueFire module. Only available on the server.
303- * @internal
304- */
305- firebaseAdmin ?: VueFireNuxtModuleOptions [ 'admin' ]
306- }
306+ /**
307+ * VueFireNuxt options used within plugins.
308+ * @internal
309+ */
310+ vuefireOptions : Pick < VueFireNuxtModuleOptions , 'appCheck' | 'auth' >
311+ }
312+
313+ declare module '@nuxt/schema' {
314+ export interface AppConfig extends VueFireAppConfig { }
315+ export interface RuntimeConfig extends VueFireRuntimeConfig { }
307316}
308317
309318// @ts -ignore: #app not found error when building
0 commit comments