-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
339 lines (294 loc) · 10.2 KB
/
Copy pathmain.js
File metadata and controls
339 lines (294 loc) · 10.2 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
const { app, BrowserWindow, Menu, ipcMain } = require('electron');
const path = require('path');
const Database = require('better-sqlite3');
const bcrypt = require('bcryptjs');
const { autoUpdater } = require('electron-updater');
let win;
const isDebugging = false;
const saltRounds = 10;
if (isDebugging) {
// Log updates to a file for debugging
autoUpdater.logger = require("electron-log");
autoUpdater.logger.transports.file.level = "info";
}
function sendStatus(message) {
if (win && win.webContents) {
win.webContents.send('status-message', message);
}
}
function sendDatabaseStatus(status) {
if (win && win.webContents) {
win.webContents.send('database-status-message', status);
}
}
function createWindow() {
app.setAppUserModelId('My Safe Data');
const appPath = app.getPath('userData');
const dbPath = path.join(appPath, 'safe-data.db');
const db = new Database(dbPath, { fileMustExist: false });
try {
db.prepare('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, data TEXT, instant DATETIME DEFAULT CURRENT_TIMESTAMP)').run();
db.prepare('CREATE TABLE IF NOT EXISTS contacts (id INTEGER PRIMARY KEY, name TEXT, data TEXT, instant DATETIME DEFAULT CURRENT_TIMESTAMP)').run();
db.prepare('CREATE TABLE IF NOT EXISTS passwords (id INTEGER PRIMARY KEY, name TEXT, data TEXT, instant DATETIME DEFAULT CURRENT_TIMESTAMP)').run();
db.prepare('CREATE TABLE IF NOT EXISTS links (id INTEGER PRIMARY KEY, name TEXT, data TEXT, instant DATETIME DEFAULT CURRENT_TIMESTAMP)').run();
db.prepare('CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, name TEXT, data TEXT, instant DATETIME DEFAULT CURRENT_TIMESTAMP)').run();
sendDatabaseStatus(true);
} catch (error) {
enviarStatus(error.message ?? error);
sendDatabaseStatus(false);
}
/*
AUTHENTICATE
*/
ipcMain.handle('authenticate', (event, dto) => {
try {
//const data = db.prepare("SELECT * FROM users where name = ? and data = ?").all(dto.name, dto.data);
const user = db.prepare("SELECT * FROM users WHERE name = ?").get(dto.name);
if (!user) {
sendStatus('User not found!');
return [];
}
const isMatch = bcrypt.compareSync(dto.data, user.data);
if (isMatch) {
sendStatus('Authenticated.');
sendDatabaseStatus(true);
delete(user.data);
return user;
} else {
sendStatus('Password mismatch.');
return [];
}
} catch (error) {
sendDatabaseStatus(false);
sendStatus('Ops: ' + error.message);
}
});
/*
USER
*/
ipcMain.handle('add-user', (event, dto) => {
try {
const hashedPassword = bcrypt.hashSync(dto.data, saltRounds);
const info = db.prepare('INSERT INTO users (name, data) VALUES (?, ?)').run(dto.name, hashedPassword);
sendStatus('User saved');
sendDatabaseStatus(true);
return info.lastInsertRowid;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
}
});
ipcMain.handle('get-user', () => {
try {
const data = db.prepare('SELECT * FROM users limit 1').all();
if (data?.length > 0) {
sendStatus('There is a registered user.');
} else {
sendStatus('You need register first.');
}
sendDatabaseStatus(true);
return data;
} catch (error) {
sendDatabaseStatus(false);
sendStatus('Ops: ' + error.message);
}
});
/*
CONTACTS
*/
ipcMain.handle('add-contact', (event, dto) => {
try {
const info = db.prepare('INSERT INTO contacts (name, data) VALUES (?, ?)').run(dto.name, dto.data);
sendStatus('Contact saved');
sendDatabaseStatus(true);
return info.lastInsertRowid;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
}
});
ipcMain.handle('get-contacts', () => {
try {
const data = db.prepare('SELECT * FROM contacts').all();
sendDatabaseStatus(true);
return data;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
}
});
ipcMain.handle('delete-contact', (event, id) => {
try {
db.prepare('DELETE FROM contacts WHERE id = ?').run(id);
sendStatus('Contact deleted.');
sendDatabaseStatus(true);
return true;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
return false;
}
});
/*
LINKS
*/
ipcMain.handle('add-bookmark', (event, dto) => {
try {
const info = db.prepare('INSERT INTO links (name, data) VALUES (?, ?)').run(dto.name, dto.data);
sendStatus('Bookmark saved');
sendDatabaseStatus(true);
return info.lastInsertRowid;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
}
});
ipcMain.handle('get-bookmarks', () => {
try {
const data = db.prepare('SELECT * FROM links').all();
sendDatabaseStatus(true);
return data;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
}
});
ipcMain.handle('delete-bookmark', (event, id) => {
try {
db.prepare('DELETE FROM links WHERE id = ?').run(id);
sendStatus('Bookmark deleted.');
sendDatabaseStatus(true);
return true;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
return false;
}
});
/*
NOTES
*/
ipcMain.handle('add-note', (event, dto) => {
try {
const info = db.prepare('INSERT INTO notes (name, data) VALUES (?, ?)').run(dto.name, dto.data);
sendStatus('Note saved');
sendDatabaseStatus(true);
return info.lastInsertRowid;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
}
});
ipcMain.handle('get-notes', () => {
try {
const data = db.prepare('SELECT * FROM notes').all();
sendDatabaseStatus(false);
return data;
} catch (error) {
sendStatus('Ops: ' + error.message);
}
});
ipcMain.handle('delete-note', (event, id) => {
try {
db.prepare('DELETE FROM notes WHERE id = ?').run(id);
sendStatus('Note deleted.');
sendDatabaseStatus(true);
return true;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
return false;
}
});
/*
PASSWORDS
*/
ipcMain.handle('add-password', (event, dto) => {
try {
const info = db.prepare('INSERT INTO passwords (name, data) VALUES (?, ?)').run(dto.name, dto.data);
sendStatus('Password saved');
sendDatabaseStatus(true);
return info.lastInsertRowid;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
}
});
ipcMain.handle('get-passwords', () => {
try {
const data = db.prepare('SELECT * FROM passwords').all();
sendDatabaseStatus(true);
return data;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
}
});
ipcMain.handle('delete-password', (event, id) => {
try {
db.prepare('DELETE FROM passwords WHERE id = ?').run(id);
sendStatus('Password deleted.');
sendDatabaseStatus(true);
return true;
} catch (error) {
sendStatus('Ops: ' + error.message);
sendDatabaseStatus(false);
return false;
}
});
win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'src/favicon.ico'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
// 1. Check for updates after the window is shown
win.once('ready-to-show', () => {
if (app.isPackaged && !isDebugging) {
autoUpdater.checkForUpdatesAndNotify();
} else {
console.log("Dev mode: auto-update skipped.");
}
});
// 2. Notify the Angular frontend about the update progress
autoUpdater.on('update-available', () => {
win.webContents.send('update_available');
});
autoUpdater.on('update-downloaded', () => {
win.webContents.send('update_downloaded');
});
if (process.platform === 'linux') {
win.setIcon(path.join(__dirname, 'src/favicon.png'));
}
ipcMain.on('restart_app', () => {
autoUpdater.quitAndInstall();
});
/*
TO DEBUG
*/
if (isDebugging) {
win.webContents.openDevTools();
}
win.loadFile(path.join(__dirname, `dist/safe-data/browser/index.html`));
win.on('closed', () => {
win = null;
});
const template = [
{
label: 'Options',
submenu: [
{ label: 'Quit', role: 'quit' }
]
}
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});