-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase-admin.config.ts
49 lines (41 loc) · 1.22 KB
/
firebase-admin.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import admin from "firebase-admin"
interface FirebaseAdminAppParams {
projectId: string
clientEmail: string
storageBucket: string
privateKey: string
}
function formatPrivateKey(key: string) {
return key.replace(/\\n/g, "\n")
}
export function createFirebaseAdminApp(
params: FirebaseAdminAppParams
) {
const privateKey = formatPrivateKey(params.privateKey)
if (admin.apps.length > 0) {
return admin.app()
}
const cert = admin.credential.cert({
projectId: params.projectId,
clientEmail: params.clientEmail,
privateKey,
})
return admin.initializeApp({
credential: cert,
projectId: params.projectId,
storageBucket: params.storageBucket,
})
}
export async function initAdmin() {
const params = {
projectId: process.env
.NEXT_PUBLIC_FIREBASE_PROJECT_ID as string,
clientEmail: process.env
.FIREBASE_CLIENT_EMAIL as string,
storageBucket: process.env
.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET as string,
privateKey: process.env
.FIREBASE_PRIVATE_KEY as string,
}
return createFirebaseAdminApp(params)
}