-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
286 lines (265 loc) · 10.1 KB
/
main.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const {
session,
app,
protocol,
shell,
BrowserWindow,
ipcMain,
nativeTheme,
dialog,
safeStorage,
Notification,
nativeImage
} = require("electron");
const path = require("path");
const url = require("url");
const {
signOut,
onAuthStateChanged,
signInWithRedirect,
getAuth,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
browserLocalPersistence,
setPersistence,
GoogleAuthProvider,
FacebookAuthProvider,
} = require("firebase/auth");
const analytics = require("firebase/analytics");
const firebase = require("firebase/app");
const {spawn} = require("child_process");
const keytar = require('keytar')
let pythonProcess = null;
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
// Import the functions you need from the SDKs you need
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
var admin = require("firebase-admin");
const firebaseConfig = require("./src/firebase/firebaseConfig.json");
var serviceAccount = require("./src/firebase/firebaseAdmin.json");
const firestoreConfig = {credential: admin.credential.cert(serviceAccount)};
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
// Import the functions you need from the SDKs you need
app.setName("Noduro");
function initializeFirebase() {
const firebaseapp = firebase.initializeApp(firebaseConfig);
const db = getFirestore(admin.initializeApp(firestoreConfig));
const auth = getAuth(firebaseapp);
const analytics = null;
const googleProvider = new GoogleAuthProvider();
const facebookProvider = new FacebookAuthProvider();
return { firebaseapp, db, auth, analytics, googleProvider, facebookProvider };
}
const iconPath = {
darwin: path.join(__dirname, '/src/icons/icon.icns'),
win32: path.join(__dirname, '/src/icons/icon.ico'),
linux: path.join(__dirname, '/src/icons/icon.png')
};
let mainWindow;
// https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app REMEBER THIS FO PACKAGING
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient("noduro", process.execPath, [
path.resolve(process.argv[1]),
]);
}
} else {
app.setAsDefaultProtocolClient("noduro");
}
//The window
const createWindow = () => {
const mainWindow = new BrowserWindow({
width: 1600,
height: 900,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
nativeWindowOpen: true,
nodeIntegration: true
},
zoomToPageWidth: true,
show: false,
backgroundColor: "#2e2c29",
icon: iconPath[process.platform]
});
mainWindow.maximize();
mainWindow.once("ready-to-show", () => {
mainWindow.webContents.openDevTools();
mainWindow.show();
});
mainWindow.loadFile("index.html");
// mainWindow.webContents.send('reset_scroll');
//Change the css style based on the system preference
ipcMain.handle("dark-mode:toggle", () => {
if (nativeTheme.shouldUseDarkColors) {
nativeTheme.themeSource = "light";
} else {
nativeTheme.themeSource = "dark";
}
return nativeTheme.shouldUseDarkColors;
});
ipcMain.handle("dark-mode:light", () => {
nativeTheme.themeSource = "light";
});
ipcMain.handle("dark-mode:dark", () => {
nativeTheme.themeSource = "dark";
});
ipcMain.handle("dark-mode:system", () => {
nativeTheme.themeSource = "system";
});
ipcMain.handle('firebase:delete_local', async (event) => {
try {
const service = "noduro_accounts";
const credentials = await keytar.findCredentials(service);
const deletionPromises = credentials.map((credential) => {
return keytar.deletePassword(service, credential.account);
});
await Promise.all(deletionPromises);
return true;
} catch (error) {
console.error("Error deleting local accounts:", error);
throw new Error("Failed to delete local accounts");
}
});
//Firebase
const {
firebaseapp,
db: firestore,
auth: firebaseAuth,
analytics: firebaseAnalytics,
googleProvider: GoogleAuthProvider,
facebookProvider: FacebookAuthProvider,
} = initializeFirebase();
// Add a new document in collection "cities" with ID 'LA'
ipcMain.handle("firebase:get_last_login", async(event, email) => {
return await keytar.getPassword('noduro_accounts', email + "_time");
});
ipcMain.handle("firebase:check_user_persist", async (event,email) => {
try{
const secret = await keytar.getPassword('noduro_accounts', email);
return [true, secret];
}
catch(error){
return [false, error];
}
});
ipcMain.handle("firebase:email_sign_up", async (event, email, password, first_name, last_name, date_of_birth) => {
try{
const user_cred = await createUserWithEmailAndPassword(firebaseAuth, email, password);
const userRef = await firestore.collection('users').doc(user_cred.user.uid).set({
email_address: email,
first_name: first_name,
last_name: last_name,
date_of_birth: date_of_birth,
})
await keytar.setPassword('noduro_accounts', email, password);
await keytar.setPassword('noduro_accounts', email + "_time", Date.now().toString());
const user = JSON.stringify(user_cred.user);
return [true, user];
}
catch(error) {
const errorCode = error.code;
const errorMessage = error.message;
// ..
return [false, error];
}
});
ipcMain.handle("firebase:email_sign_in", async (event, email, password,user_signing_in) => {
try {
await setPersistence(firebaseAuth, browserLocalPersistence);
const userCredential = await signInWithEmailAndPassword(firebaseAuth, email, password);
keytar.setPassword('noduro_accounts', email, password);
if (user_signing_in){
await keytar.setPassword('noduro_accounts', email + "_time", Date.now().toString());
}
const user = JSON.stringify(userCredential.user);
return [true, user];
} catch (error) {
return [false, error];
}
});
ipcMain.handle("firebase:get_current_user", async (event) => {
return new Promise((resolve) => {
onAuthStateChanged(firebaseAuth, (user) => {
if (user) {
const user_send = JSON.stringify(user);
resolve([true, user_send]);
} else {
resolve([false, "user_not_found"]);
}
});
});
});
ipcMain.handle("firebase:get_current_user_information", async (event,user) => {
const userRef = await firestore.collection('users').doc(user.uid).get();
if (userRef.exists) {
// var x = {...user, ...userRef.data()}
return [true, {...user, ...userRef.data()}];
}
else {
return [false, "user_firestore_data_not_found"];
}
});
ipcMain.handle("firebase:email_sign_out", async (event, email) => {
try{
signOut(firebaseAuth)
keytar.deletePassword('noduro_accounts', email);
return true;
}
catch(error){
return error;
}
});
ipcMain.on('start-python-script', async (event, arg) => {
pythonProcess = spawn('python3', [path.join(__dirname, 'python/run.py')])
});
};
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
}
else {
app.on("second-instance", (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
// the commandLine is array of strings in which last element is deep link url
// the url str ends with /
dialog.showErrorBox(
"Welcome Back",
`You arrived from: ${commandLine.pop().slice(0, -1)}`
);
});
// Create mainWindow, load the rest of the app, etc...
app.whenReady().then(() => {
const platform = process.platform;
if (platform === 'darwin'){
var icon_img = iconPath.darwin; app.dock.setIcon(nativeImage.createFromPath(icon_img));
}
else if (platform === 'win32') var icon_img = iconPath.win32;
else if (platform === 'linux') var icon_img = iconPath.linux;
createWindow();
protocol.registerFileProtocol("noduro", (request, callback) => {
const filePath = url.fileURLToPath(
"file://" + request.url.slice("noduro://".length)
);
callback(filePath);
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
}
app.on("open-url", (event, url) => {
dialog.showErrorBox("Welcome Back", `You arrived from: ${url}`);
});
app.on("window-all-closed", () => {
pythonProcess.kill();
if (process.platform !== "darwin") {
app.quit();
}
});