Skip to content

Commit 8d2aa52

Browse files
committed
refactor(nuxt): better options and wip app check
1 parent 0dd82d3 commit 8d2aa52

File tree

6 files changed

+97
-36
lines changed

6 files changed

+97
-36
lines changed

packages/nuxt/playground/nuxt.config.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ export default defineNuxtConfig({
2121
modules: [
2222
//
2323
[
24-
VueFire,
24+
'../src/module.ts',
2525
{
26-
services: {
27-
auth: true,
26+
auth: true,
27+
appCheck: {
28+
debug: process.env.NODE_ENV !== 'production',
29+
isTokenAutoRefreshEnabled: true,
30+
provider: 'RecaptchaV3',
31+
key: '6LfJ0vgiAAAAAHheQE7GQVdG_c9m8xipBESx_SKI',
2832
},
33+
2934
config: {
3035
apiKey: 'AIzaSyAkUKe36TPWL2eZTshgk-Xl4bY_R5SB97U',
3136
authDomain: 'vue-fire-store.firebaseapp.com',

packages/nuxt/playground/pages/database/todos.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<script setup lang="ts">
2-
const todos = []
2+
const todos = ref<{ title: string, id: string }[]>([])
33
</script>
44

55
<template>
66
<div>
77
<h1>Todos</h1>
88
<ul>
99
<li v-for="todo in todos" :key="todo.id">
10-
<NuxtLink :to="`/database/todos/${todo.id}`">{{ todo.title }}</NuxtLink>
10+
<NuxtLink :to="`/database/todos/${todo.id}`">
11+
{{ todo.title }}
12+
</NuxtLink>
1113
</li>
1214
</ul>
1315
</div>

packages/nuxt/playground/pages/index.vue

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
<script lang="ts">
2-
import {} from 'firebase/firestore'
3-
// import { listAll, ref as storageRef } from 'firebase/storage'
4-
// import { useFirebaseApp, useStorage } from 'vuefire'
5-
6-
console.log('initializing pages/index.vue')
7-
// console.log('firebaseApp', !!useFirebaseApp())
8-
9-
// listAll(storageRef(useStorage(), 'demo')).then((data) => {
10-
// console.log('data', data)
11-
// })
12-
</script>
1+
<script lang="ts"></script>
132

143
<template>
154
<div>

packages/nuxt/src/app-check/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { VueFireAppCheckOptions } from 'vuefire'
2+
3+
/**
4+
* @internal
5+
*/
6+
export interface _NuxtVueFireAppCheckOptionsBase
7+
extends Omit<VueFireAppCheckOptions, 'provider'> {
8+
provider: 'ReCaptchaV3' | 'ReCaptchaEnterprise' | 'Custom'
9+
}
10+
11+
export interface NuxtVueFireAppCheckOptionsReCaptchaV3
12+
extends _NuxtVueFireAppCheckOptionsBase {
13+
provider: 'ReCaptchaV3'
14+
key: string
15+
}
16+
17+
export interface NuxtVueFireAppCheckOptionsReCaptchaEnterprise
18+
extends _NuxtVueFireAppCheckOptionsBase {
19+
provider: 'ReCaptchaEnterprise'
20+
key: string
21+
}
22+
23+
// TODO: Custom provider
24+
25+
export type NuxtVueFireAppCheckOptions =
26+
| NuxtVueFireAppCheckOptionsReCaptchaV3
27+
| NuxtVueFireAppCheckOptionsReCaptchaEnterprise

packages/nuxt/src/module.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
defineNuxtModule,
88
} from '@nuxt/kit'
99
import type { NuxtModule } from '@nuxt/schema'
10-
import { type FirebaseOptions } from '@firebase/app-types'
10+
import type { FirebaseOptions } from '@firebase/app-types'
11+
import type { NuxtVueFireAppCheckOptions } from './app-check'
1112

1213
export interface VueFireNuxtModuleOptions {
1314
/**
@@ -17,19 +18,22 @@ export interface VueFireNuxtModuleOptions {
1718
*/
1819
optionsApiPlugin?: boolean | 'firestore' | 'database'
1920

20-
config: FirebaseOptions
21+
config?: FirebaseOptions
2122

2223
/**
2324
* Optional name passed to `firebase.initializeApp(config, name)`
2425
*/
2526
appName?: string
2627

27-
services?: {
28-
auth?: boolean
29-
firestore?: boolean
30-
database?: boolean
31-
storage?: boolean
32-
}
28+
/**
29+
* Enables AppCheck
30+
*/
31+
appCheck?: NuxtVueFireAppCheckOptions
32+
33+
/**
34+
* Enables Authentication
35+
*/
36+
auth?: boolean
3337
}
3438

3539
// Manual to avoid build error
@@ -45,11 +49,16 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
4549

4650
defaults: {
4751
optionsApiPlugin: false,
48-
config: {},
49-
services: {},
5052
},
5153

5254
setup(options, nuxt) {
55+
// ensure provided options are valid
56+
if (!options.config) {
57+
throw new Error(
58+
'[VueFire]: Missing firebase config. Provide a "config" option to the VueFire module options.'
59+
)
60+
}
61+
5362
const { resolve } = createResolver(import.meta.url)
5463
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
5564
const templatesDir = fileURLToPath(
@@ -58,12 +67,7 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
5867

5968
// Let plugins and the user access the firebase config within the app
6069
nuxt.options.appConfig.firebaseConfig = options.config
61-
62-
if (Object.keys(options.config).length === 0) {
63-
throw new Error(
64-
'[VueFire]: Missing firebase config. Provide it to the VueFire module options.'
65-
)
66-
}
70+
nuxt.options.appConfig.appCheck = options.appCheck
6771

6872
// nuxt.options.build.transpile.push(templatesDir)
6973
nuxt.options.build.transpile.push(runtimeDir)
@@ -83,12 +87,22 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
8387
})
8488

8589
export default VueFire
90+
export type {
91+
NuxtVueFireAppCheckOptions,
92+
NuxtVueFireAppCheckOptionsReCaptchaV3,
93+
NuxtVueFireAppCheckOptionsReCaptchaEnterprise,
94+
} from './app-check'
8695

8796
declare module '@nuxt/schema' {
8897
export interface AppConfig {
8998
/**
9099
* Firebase config to initialize the app.
91100
*/
92101
firebaseConfig: FirebaseOptions
102+
103+
/**
104+
* AppCheck options passed to VueFire module.
105+
*/
106+
appCheck?: NuxtVueFireAppCheckOptions
93107
}
94108
}

packages/nuxt/templates/plugin.ejs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { usePendingPromises, VueFire, useSSRInitialState } from 'vuefire'
1+
import {
2+
usePendingPromises,
3+
VueFire,
4+
5+
useSSRInitialState,
6+
7+
// TODO: conditional imports
8+
VueFireAppCheck,
9+
} from 'vuefire'
210
import { initializeApp } from 'firebase/app'
311
import {
412
defineNuxtPlugin,
@@ -16,22 +24,38 @@ getDatabase,
1624
import {
1725
toRaw,
1826
} from 'vue-demi'
27+
import {
28+
// TODO: conditional imports
29+
ReCaptchaV3Provider,
30+
ReCaptchaEnterpriseProvider,
31+
CustomProvider,
32+
} from 'firebase/app-check'
1933

2034
export default defineNuxtPlugin((nuxtApp) => {
2135
const appConfig = useAppConfig()
2236
const firebaseConfig = toRaw(appConfig).firebaseConfig
2337

2438
const firebaseApp = initializeApp(firebaseConfig)
2539

40+
console.log('appcheck debug', appConfig.appCheck.debug)
41+
42+
// TODO: emulator option
2643
// connectFirestoreEmulator(getFirestore(firebaseApp), 'localhost', 8080)
2744
// connectDatabaseEmulator(getDatabase(firebaseApp), 'localhost', 8081)
2845

2946
nuxtApp.vueApp.use(VueFire, {
3047
firebaseApp,
31-
services: [
32-
<% if(options.services.auth) { %>
48+
modules: [
49+
<% if(options.auth) { %>
3350
// Auth
3451
<% } %>
52+
<% if(options.appCheck) { %>
53+
VueFireAppCheck({
54+
debug: <%= options.appCheck.debug %>,
55+
isTokenAutoRefreshEnabled: <%= options.appCheck.isTokenAutoRefreshEnabled %>,
56+
provider: new ReCaptchaV3Provider('<%= options.appCheck.key %>'),
57+
}),
58+
<% } %>
3559
],
3660
})
3761

0 commit comments

Comments
 (0)