Skip to content

Commit bb9f2fd

Browse files
committed
feat(nuxt): add auto imports
1 parent 5add0af commit bb9f2fd

File tree

5 files changed

+85
-30
lines changed

5 files changed

+85
-30
lines changed

packages/nuxt/playground/app.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const routes = router
1919
<div>
2020
<nav class="nav-links">
2121
<ul>
22+
<li>
23+
<NuxtLink to="/">
24+
Home
25+
</NuxtLink>
26+
</li>
2227
<li v-for="route in routes">
2328
<NuxtLink :to="route.to">
2429
{{ route.label }}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getCurrentUser } from 'vuefire'
2+
export default defineNuxtRouteMiddleware(async (to, from) => {
3+
const app = useNuxtApp().$firebaseApp
4+
// TODO: handle
5+
if (process.server) {
6+
return
7+
}
8+
const user = await getCurrentUser(app.name)
9+
console.log('user', user)
10+
11+
if (!user) {
12+
return navigateTo('/authentication')
13+
}
14+
})

packages/nuxt/playground/pages/firestore-secure-doc.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<script setup lang="ts">
2-
import { doc, getDoc } from 'firebase/firestore'
3-
import { useCurrentUser, useDocument, useFirestore, usePendingPromises } from 'vuefire'
4-
import { ref } from 'vue'
2+
import { doc } from 'firebase/firestore'
3+
import { useCurrentUser, useFirestore, usePendingPromises } from 'vuefire'
4+
5+
definePageMeta({
6+
middleware: ['vuefire-auth']
7+
})
58
69
const db = useFirestore()
710
const user = useCurrentUser()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default defineNuxtPlugin((nuxt) => {
2+
const firebaseApp = useNuxtApp().$firebaseApp
3+
console.log('Has firebase App', !!firebaseApp)
4+
})

packages/nuxt/src/module.ts

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fileURLToPath } from 'node:url'
22
import { normalize } from 'node:path'
33
import {
4+
addImports,
45
addPlugin,
56
addPluginTemplate,
67
addServerHandler,
@@ -127,43 +128,71 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
127128
})
128129
}
129130

130-
// this allows us to the order of the plugins
131-
nuxt.hook('modules:done', () => {
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-
}
131+
// NOTE: the order of the plugins is reversed, so we end by adding the app plugin which is used by all other
132+
// plugins
137133

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-
}
134+
if (options.auth) {
135+
addPlugin(resolve(runtimeDir, 'auth/plugin.client'))
136+
// must be added after the admin module to use the admin app
137+
addPlugin(resolve(runtimeDir, 'auth/plugin.server'))
146138

147-
if (options.appCheck) {
148-
addPlugin(resolve(runtimeDir, 'app-check/plugin'))
139+
addVueFireImports([
140+
// auth
141+
{ from: 'vuefire', name: 'useFirebaseAuth' },
142+
{ from: 'vuefire', name: 'useCurrentUser' },
143+
])
144+
}
145+
146+
if (options.admin) {
147+
if (!nuxt.options.ssr) {
148+
console.warn(
149+
'[VueFire]: The "admin" option is only used during SSR. You should reenable ssr to use it.'
150+
)
149151
}
152+
addPlugin(resolve(runtimeDir, 'admin/plugin.server'))
153+
}
150154

151-
// plugin are added in reverse order
152-
addPluginTemplate({
153-
src: normalize(resolve(templatesDir, 'plugin.ejs')),
155+
if (options.appCheck) {
156+
addPlugin(resolve(runtimeDir, 'app-check/plugin'))
157+
}
154158

155-
options: {
156-
...options,
157-
ssr: nuxt.options.ssr,
158-
},
159-
})
159+
// plugin are added in reverse order
160+
addPluginTemplate({
161+
src: normalize(resolve(templatesDir, 'plugin.ejs')),
160162

161-
// adds the firebase app to each application
162-
addPlugin(resolve(runtimeDir, 'app/plugin'))
163+
options: {
164+
...options,
165+
ssr: nuxt.options.ssr,
166+
},
163167
})
168+
169+
// adds the firebase app to each application
170+
addPlugin(resolve(runtimeDir, 'app/plugin'))
171+
172+
addVueFireImports([
173+
// firestore
174+
{ from: 'vuefire', name: 'useDocument' },
175+
{ from: 'vuefire', name: 'useCollection' },
176+
{ from: 'vuefire', name: 'useFirestore' },
177+
178+
// database
179+
{ from: 'vuefire', name: 'useDatabase' },
180+
{ from: 'vuefire', name: 'useDatabaseList' },
181+
{ from: 'vuefire', name: 'useDatabaseObject' },
182+
])
164183
},
165184
})
166185

186+
type VueFireModuleExportKeys = keyof Awaited<typeof import('vuefire')>
187+
function addVueFireImports(
188+
imports: Array<{
189+
from: 'vuefire'
190+
name: VueFireModuleExportKeys
191+
}>
192+
) {
193+
return addImports(imports)
194+
}
195+
167196
export default VueFire
168197
export type {
169198
NuxtVueFireAppCheckOptions,

0 commit comments

Comments
 (0)