Skip to content

Commit 95ea119

Browse files
committed
feat(app-check): useAppCheckToken()
1 parent 895af65 commit 95ea119

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

playground/src/main.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { createApp } from 'vue'
22
import { createPinia } from 'pinia'
3-
import { firestorePlugin, VueFire, VueFireAuth } from 'vuefire'
3+
import { firestorePlugin, VueFire, VueFireAuth, VueFireAppCheck } from 'vuefire'
44
import App from './App.vue'
55
import { createFirebaseApp } from './firebase'
66
import { createWebHistory, createRouter } from 'vue-router/auto'
77
import { createStore } from 'vuex'
8+
import { ReCaptchaV3Provider } from 'firebase/app-check'
89

910
const router = createRouter({
1011
history: createWebHistory(),
@@ -19,12 +20,23 @@ const store = createStore({
1920
}),
2021
})
2122

23+
// @ts-expect-error: ok
24+
self.FIREBASE_APPCHECK_DEBUG_TOKEN = true
25+
2226
const app = createApp(App)
2327
app
2428
.use(createPinia())
2529
.use(VueFire, {
2630
firebaseApp: createFirebaseApp(),
27-
modules: [VueFireAuth],
31+
modules: [
32+
VueFireAuth(),
33+
VueFireAppCheck({
34+
isTokenAutoRefreshEnabled: true,
35+
provider: new ReCaptchaV3Provider(
36+
'6LfJ0vgiAAAAAHheQE7GQVdG_c9m8xipBESx_SKI'
37+
),
38+
}),
39+
],
2840
})
2941
.use(firestorePlugin)
3042
.use(store)

playground/typed-router.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ import type {
3636
declare module 'vue-router/auto/routes' {
3737
export interface RouteNamedMap {
3838
'/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>
39+
'/app-check': RouteRecordInfo<
40+
'/app-check',
41+
'/app-check',
42+
Record<never, never>,
43+
Record<never, never>
44+
>
3945
'/authentication': RouteRecordInfo<
4046
'/authentication',
4147
'/authentication',

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 { FirebaseApp } from 'firebase/app'
2+
import {
3+
initializeAppCheck,
4+
AppCheckOptions,
5+
onTokenChanged,
6+
} from 'firebase/app-check'
7+
import { App, inject, InjectionKey, Ref, ref } from 'vue'
8+
import { scope } from '../globals'
9+
10+
export const AppCheckTokenInjectSymbol: InjectionKey<Ref<string | undefined>> =
11+
Symbol('app-check-token')
12+
13+
export function useAppCheckToken() {
14+
return inject(AppCheckTokenInjectSymbol)!
15+
}
16+
17+
export function VueFireAppCheck(options: AppCheckOptions) {
18+
return (firebaseApp: FirebaseApp | undefined, app: App) => {
19+
const appCheck = initializeAppCheck(firebaseApp, options)
20+
const token = scope.run(() => ref<string>())!
21+
onTokenChanged(appCheck, (newToken) => {
22+
token.value = newToken.token
23+
})
24+
25+
app.provide(AppCheckTokenInjectSymbol, token)
26+
}
27+
}

src/auth/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ import { AuthUserInjectSymbol, setupOnAuthStateChanged } from './user'
77

88
export { setupOnAuthStateChanged, useCurrentUser } from './user'
99

10-
// TODO: figure out a way, could be related to adding a new API to Vue that allows library authors to set the active app and therefore allow top level injcections
11-
// const userMap = new WeakMap<FirebaseApp, Ref<User | null | undefined>>()
10+
export function VueFireAuth(_app?: never) {
11+
// TODO: refactor to share across modules
12+
if (process.env.NODE_ENV !== 'production') {
13+
if (_app != null) {
14+
console.warn(`Did you forget to call the VueFireAuth function? It should look like
15+
modules: [VueFireAuth()]`)
16+
}
17+
}
1218

13-
export function VueFireAuth(firebaseApp: FirebaseApp | undefined, app: App) {
14-
const user = scope.run(() => shallowRef<User | null | undefined>())!
15-
// userMap.set(app, user)
16-
app.provide(AuthUserInjectSymbol, user)
17-
setupOnAuthStateChanged(user, firebaseApp)
19+
return (firebaseApp: FirebaseApp | undefined, app: App) => {
20+
const user = scope.run(() => shallowRef<User | null | undefined>())!
21+
// userMap.set(app, user)
22+
app.provide(AuthUserInjectSymbol, user)
23+
setupOnAuthStateChanged(user, firebaseApp)
24+
}
1825
}
1926

2027
/**

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export { useCurrentUser, VueFireAuth, useFirebaseAuth } from './auth'
5252
// SSR
5353
export { usePendingPromises } from './ssr/plugin'
5454

55+
// App Check
56+
export { VueFireAppCheck, useAppCheckToken } from './app-check'
57+
5558
/**
5659
* Options for VueFire Vue plugin.
5760
*/

0 commit comments

Comments
 (0)