File size: 1,544 Bytes
ea81969
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import admin from 'firebase-admin';
import fs from 'fs';
import path from 'path';

// Service Account file path - MUST be from transcript-master project
const serviceAccountPath = path.resolve(process.cwd(), 'service-account.json');

try {
    if (!admin.apps.length) {
        // Explicitly using transcript-master project details
        const config = {
            databaseURL: "https://klingwatermark-default-rtdb.asia-southeast1.firebasedatabase.app",
            projectId: "klingwatermark"
        };

        if (fs.existsSync(serviceAccountPath)) {
            const serviceAccount = JSON.parse(fs.readFileSync(serviceAccountPath, 'utf8'));
            // Safety check: ensure the file matches the database project
            if (serviceAccount.project_id !== "transcript-master") {
                console.error(`❌ CRITICAL: service-account.json is for project "${serviceAccount.project_id}", but database is "transcript-master". Please download the correct key.`);
            }
            config.credential = admin.credential.cert(serviceAccount);
            console.log("✅ Firebase Admin: Using service-account.json for transcript-master");
        } else {
            console.warn("ℹ️ Firebase Admin: service-account.json NOT FOUND. DB access will fail.");
        }

        admin.initializeApp(config);
        console.log("🚀 Firebase Admin Service Initialized for transcript-master");
    }
} catch (error) {
    console.error("❌ Firebase Init Error:", error.message);
}

export const db = admin.database();