|
1 | | -import { |
2 | | - initializeApp, |
3 | | - cert, |
4 | | - getApp, |
5 | | - getApps, |
6 | | - applicationDefault, |
7 | | - // renamed because there seems to be a global Credential type in vscode |
8 | | - Credential as FirebaseAdminCredential, |
9 | | - App as AdminApp, |
10 | | -} from 'firebase-admin/app' |
11 | | -import { log } from '../logging' |
| 1 | +import { App as AdminApp } from 'firebase-admin/app' |
| 2 | +import { getAdminApp } from 'vuefire/server' |
12 | 3 | import { defineNuxtPlugin, useAppConfig, useRequestEvent } from '#app' |
13 | 4 |
|
14 | | -export default defineNuxtPlugin((nuxtApp) => { |
| 5 | +export default defineNuxtPlugin(() => { |
15 | 6 | const event = useRequestEvent() |
16 | | - // only initialize the admin sdk once |
17 | | - if (!getApps().length) { |
18 | | - const { firebaseAdmin } = useAppConfig() |
19 | | - const { |
20 | | - // these can be set by the user on other platforms |
21 | | - FIREBASE_PROJECT_ID, |
22 | | - FIREBASE_CLIENT_EMAIL, |
23 | | - FIREBASE_PRIVATE_KEY, |
24 | | - // set on firebase cloud functions |
25 | | - FIREBASE_CONFIG, |
26 | | - // in cloud functions, we can auto initialize |
27 | | - FUNCTION_NAME, |
28 | | - GOOGLE_APPLICATION_CREDENTIALS, |
29 | | - } = process.env |
| 7 | + const { firebaseAdmin } = useAppConfig() |
30 | 8 |
|
31 | | - if (FIREBASE_CONFIG || FUNCTION_NAME) { |
32 | | - log('debug', 'using FIREBASE_CONFIG env variable') |
33 | | - initializeApp() |
34 | | - } else { |
35 | | - let credential: FirebaseAdminCredential |
36 | | - // This version should work in Firebase Functions and other providers while applicationDefault() only works on |
37 | | - // Firebase deployments |
38 | | - if (FIREBASE_PRIVATE_KEY) { |
39 | | - log('debug', 'using FIREBASE_PRIVATE_KEY env variable') |
40 | | - credential = cert({ |
41 | | - projectId: FIREBASE_PROJECT_ID, |
42 | | - clientEmail: FIREBASE_CLIENT_EMAIL, |
43 | | - // replace `\` and `n` character pairs w/ single `\n` character |
44 | | - privateKey: FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'), |
45 | | - }) |
46 | | - } else if (process.env.GOOGLE_APPLICATION_CREDENTIALS) { |
47 | | - if ( |
48 | | - typeof GOOGLE_APPLICATION_CREDENTIALS === 'string' && |
49 | | - // ensure it's an object |
50 | | - GOOGLE_APPLICATION_CREDENTIALS[0] === '{' |
51 | | - ) { |
52 | | - log( |
53 | | - 'debug', |
54 | | - 'Parsing GOOGLE_APPLICATION_CREDENTIALS env variable as JSON' |
55 | | - ) |
56 | | - credential = cert(JSON.parse(GOOGLE_APPLICATION_CREDENTIALS)) |
57 | | - } else { |
58 | | - // automatically picks up the service account file path from the env variable |
59 | | - log('debug', 'using applicationDefault()') |
60 | | - credential = applicationDefault() |
61 | | - } |
62 | | - } else { |
63 | | - // No credentials were provided, this will fail so we throw an explicit error |
64 | | - // TODO: add link to vuefire docs |
65 | | - log( |
66 | | - 'warn', |
67 | | - `\ |
68 | | -You must provide an "admin.serviceAccount" path to your json so it's picked up during development. See https://firebase.google.com/docs/admin/setup#initialize-sdk for more information. Note that you can also set the GOOGLE_APPLICATION_CREDENTIALS env variable to a full resolved path or a JSON string. |
69 | | -You can also set the FIREBASE_CLIENT_EMAIL, FIREBASE_PRIVATE_KEY and FIREBASE_PROJECT_ID env variables in production if you are deploying to something else than Firebase Cloud Functions. |
70 | | -` |
71 | | - ) |
72 | | - throw new Error('admin-app/missing-credentials') |
73 | | - } |
| 9 | + const firebaseAdminApp = getAdminApp(firebaseAdmin?.options) |
74 | 10 |
|
75 | | - initializeApp({ |
76 | | - // TODO: is this really going to be used? |
77 | | - ...firebaseAdmin?.options, |
78 | | - credential, |
79 | | - }) |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - const firebaseAdminApp = getApp() |
84 | | - // TODO: Is this accessible within middlewares and api routes? or should be use a middleware to add it |
| 11 | + // TODO: Is this accessible within middlewares and api routes? or should we use a middleware to add it |
85 | 12 | event.context.firebaseApp = firebaseAdminApp |
86 | 13 |
|
87 | 14 | return { |
|
0 commit comments