forked from Flowdesktech/firestudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebaseController.js
More file actions
100 lines (84 loc) · 2.43 KB
/
Copy pathfirebaseController.js
File metadata and controls
100 lines (84 loc) · 2.43 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Firebase Controller
* Handles Firebase Admin SDK connection and disconnection
*/
const { ipcMain, dialog } = require('electron');
const fs = require('fs');
let admin = null;
let db = null;
let onConnectionChange = null;
function getAdmin() {
return admin;
}
function getDb() {
return db;
}
/**
* Sets callback to notify when connection changes
*/
function setConnectionChangeCallback(callback) {
onConnectionChange = callback;
}
/**
* Registers Firebase connection IPC handlers
*/
function registerHandlers() {
// Connect to Firebase with service account
ipcMain.handle('firebase:connect', async (event, params) => {
try {
// Support both object params and legacy string path
const serviceAccountPath = typeof params === 'string' ? params : params.serviceAccountPath;
const databaseId = typeof params === 'string' ? undefined : params.databaseId;
if (admin) {
await admin.app().delete();
}
const serviceAccount = JSON.parse(fs.readFileSync(serviceAccountPath, 'utf8'));
admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
db = admin.firestore();
if (databaseId) {
db.settings({ databaseId });
}
// Notify other controllers about the connection change
if (onConnectionChange) {
onConnectionChange(admin, db);
}
return { success: true, projectId: serviceAccount.project_id, databaseId };
} catch (error) {
return { success: false, error: error.message };
}
});
// Disconnect from Firebase
ipcMain.handle('firebase:disconnect', async () => {
try {
if (admin) {
await admin.app().delete();
admin = null;
db = null;
// Notify other controllers about the disconnection
if (onConnectionChange) {
onConnectionChange(null, null);
}
}
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
});
// Open file dialog for service account
ipcMain.handle('dialog:openFile', async () => {
const { filePaths } = await dialog.showOpenDialog({
filters: [{ name: 'JSON Files', extensions: ['json'] }],
properties: ['openFile'],
});
return filePaths && filePaths.length > 0 ? filePaths[0] : null;
});
}
module.exports = {
registerHandlers,
getAdmin,
getDb,
setConnectionChangeCallback,
};