-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
289 lines (201 loc) · 7.66 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
287
288
289
console.log("Main process started");
const path = require("path");
const url = require("url");
const electron = require("electron");
const app = electron.app;
const ipc = electron.ipcMain;
const bcrypt = require("bcrypt-nodejs");
let win;
function createWindow() {
win = new electron.BrowserWindow({frame: false, width: 1200, show: true, minWidth: 800});
// global variable holding username of the logged in user
global.user = {
username: null
}
win.loadURL(url.format({
// pathname: path.join(__dirname, "static", "html", "home.html"),
pathname: path.join(__dirname, "static", "html", "login.html"),
slashes: true,
protocol: "file"
}));
win.on("closed", () => {
// make window block in heap unreachable so that, that will be garbage collected
win = null;
})
}
app.on("ready", createWindow);
// closing window
ipc.on("close-window", function(event) {
console.log("window close request");
win.close();
});
// --------------------- DATABASE ------------------------------
const db_connection = require("./database_handlers/db_connection");
const db_insert = require("./database_handlers/db_insert");
const db_select = require("./database_handlers/db_select");
const db_delete = require("./database_handlers/db_delete");
const db_update = require("./database_handlers/db_update");
const connection = db_connection.connect();
ipc.on("register_account", function (event, data) {
console.log("account to be registered");
// Escaping to prevent SQL INJECTION
let username = connection.escape(data.username);
let password = connection.escape(data.password);
console.log(username, password);
bcrypt.hash(password, null, null, function(err, hash) {
console.log(password + ":", hash);
let query_string =
`
INSERT INTO users
(username, password)
VALUES
(${username}, '${hash}');
`;
console.log(query_string);
connection.query(query_string, function (error, results, fields) {
if (error) {
if (error.code == "ER_DUP_ENTRY") {
console.log("duplicate entry error");
event.sender.send("account_already_exists_error");
return;
}
throw error;
}
event.sender.send("account_registered");
});
});
});
ipc.on("account_login", function (event, data) {
// Security against SQL Injection
let username = connection.escape(data.username);
let query_string =
`
SELECT username, password
FROM users
WHERE username = ${username}
`;
console.log(query_string);
connection.query(query_string, function (error, results, fields) {
if (error) throw error;
console.log(results);
if (results.length <= 0) {
console.log("User doesn't exist");
event.sender.send("login_error", {msg: "There is no account with that username"});
return;
}
let pwd_hash = results[0].password;
let pwd = connection.escape(data.password);
bcrypt.compare(pwd, pwd_hash, function (error, res) {
if (res === false) {
event.sender.send("login_error", {msg: "Incorrect! Password"});
return;
}
if (res === true) {
console.log("successful login");
global.user.username = data.username;
event.sender.send("login_success", {username: data.username});
}
});
});
});
ipc.on("data-stream", function (event, data){
console.log("NOTE: data-stream message from renderer", data);
for (let i=0; i<data.length; i++) {
console.log(data[i]);
// Security against SQL Injection
let descp = connection.escape(data[i].description);
let category = data[i].category;
let catg_id = null;
connection.query(`SELECT id FROM categories WHERE name = "${category}" `, function (error, results, fields) {
if (error) throw error;
catg_id = results[0].id;
let to_insert_data = {
info: descp,
category_id: catg_id,
username: global.user.username
};
db_insert.insert(connection, "records", to_insert_data, function (error) {
if (error) throw error;
});
});
}
});
ipc.on("query_data", function(event, data) {
console.log(data);
let query_string = db_select.select_query(data.date_from, data.date_to, data.category);
console.log(query_string);
db_select.query(connection, query_string, function(error, response_data) {
if (error) throw error;
event.sender.send("database_data", response_data);
});
});
ipc.on("delete-record", function (event, record_id) {
let query_string = db_delete.delete_query(record_id);
console.log(query_string);
db_delete.delete(connection, query_string, function (error) {
if (error) throw error;
});
event.sender.send("record-deleted", record_id);
});
ipc.on("update-record", function (event, updated_data) {
console.log(`updated_data: ${updated_data.description}, category: ${updated_data.category}`);
// Security against SQL Injection
let description = connection.escape(updated_data.description);
let record_id = updated_data.record_id;
let category = updated_data.category;
connection.query(`SELECT id FROM categories WHERE name = "${category}" `, function (error, results, fields) {
if (error) throw error;
catg_id = results[0].id;
let query_string = db_update.update_query(record_id, catg_id, description);
console.log(query_string);
db_update.update(connection, query_string, function (error) {
if (error) throw error;
});
event.sender.send("record-updated", updated_data);
});
});
ipc.on("change_password", function (event, data) {
console.log("request to change password");
// Security against SQL Injection
let pwd = connection.escape(data.password);
let new_pwd = connection.escape(data.new_password);
console.log(pwd, new_pwd);
let query_string =
`
SELECT password
FROM users
WHERE username = "${global.user.username}"
`;
console.log(query_string);
connection.query(query_string, function (error, results, fields) {
if (error) throw error;
let hash_pwd = results[0].password;
console.log("hash: " + hash_pwd);
bcrypt.compare(pwd, hash_pwd, function (error, res) {
if (error) throw error;
if (res === true) {
console.log("correct old password");
bcrypt.hash(new_pwd, null, null, function(err, hash){
let query_string =
`
UPDATE users SET password = "${hash}"
WHERE username = "${global.user.username}"
`;
connection.query(query_string, function (error, results, fields) {
if (error) throw error;
event.sender.send("change_password_success");
})
});
}
if (res === false) {
console.log("incorrect old password");
event.sender.send("incorrect_password");
return;
}
});
});
});
app.on("closed", function (){
console.log("closing database connection");
db_connection.disconnect();
});