-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase-admin.ts
More file actions
36 lines (31 loc) · 1.07 KB
/
firebase-admin.ts
File metadata and controls
36 lines (31 loc) · 1.07 KB
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
import {
initializeApp,
getApps,
App,
getApp,
cert,
} from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";
let app: App;
if(getApps().length === 0){
// Use environment variables for Firebase Admin configuration
const serviceAccount = {
type: "service_account",
project_id: process.env.FIREBASE_PROJECT_ID,
private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
client_email: process.env.FIREBASE_CLIENT_EMAIL,
client_id: process.env.FIREBASE_CLIENT_ID,
auth_uri: "https://accounts.google.com/o/oauth2/auth",
token_uri: "https://oauth2.googleapis.com/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
client_x509_cert_url: process.env.FIREBASE_CLIENT_X509_CERT_URL,
};
app = initializeApp({
credential: cert(serviceAccount as any),
});
} else{
app = getApp();
}
const adminDb = getFirestore(app);
export {app as adminApp, adminDb};