forked from Atishay96/DesktopChat-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
172 lines (158 loc) · 3.77 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
const electron = require('electron');
const url = require('url');
const path = require('path');
const request = require('request');
// const __appurl = "http://localhost:8080/";
const __appurl = 'http://www.atishay.tk/'
const { app, BrowserWindow, Menu, ipcMain } = electron;
let mainWindow;
let knex = require("knex")({
client: "sqlite3",
connection: {
filename: "./chat.sqlite"
}
});
process.env.NODE_ENV = 'production';
knex.schema.createTable('Users', function (table) {
table.increments('id');
table.string('authToken');
table.string('userId');
}).then();
var dir = 'login.html';
// if app is ready
app.on('ready', () => {
var result = knex.select("authToken", "userId").from("Users")
result.then(function (rows) {
console.log(rows);
if (rows.length) {
let token = (rows[rows.length - 1]).authToken;
console.log(token);
request.post({ url: __appurl + 'api/isLoggedIn', "form": { "token": token } }, function (error, response, body) {
console.log(body);
if (error) {
return console.log('error:', error); // Print the error if one occurred
}
if (response.statusCode == 200) {
dir = 'chatScreen.html';
}
done();
});
}
else {
done();
}
}).catch(function (e) {
console.error(e);
});
//create new window
})
function done() {
mainWindow = new BrowserWindow({
minHeight: 500,
minWidth: 700
});
// load html in window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, dir),
protocol: 'file:',
slashes: true
}));
mainWindow.on('closed', function () {
app.quit();
})
//build menu from template
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
//insert menu
Menu.setApplicationMenu(mainMenu);
}
ipcMain.on("storeToken", (err, token, userId) => {
let ins = knex('Users').insert({ authToken: token, userId: userId })
ins.then(function (rows) {
console.log('token stored');
mainWindow.webContents.send("chatScreen", { status: 200 });
}).catch(function (err) {
console.error(err);
});
});
ipcMain.on('getUser', (err) => {
var result = knex.select("authToken", "userId").from("Users")
result.then(function (rows) {
console.log('sent');
mainWindow.webContents.send("userDetails", rows[rows.length - 1]);
}).catch(function (e) {
console.error(e);
});
})
function logout() {
knex('Users').del().then(function(rows){
console.log('Logged Out');
dir = 'login.html';
done();
})
}
const mainMenuTemplate = [
{
// label: 'File',
// submenu: [
// // {
// // label: 'Add Item',
// // accelerator: process.platform == 'darwin' ? 'Command+D' : 'Ctrl+D',
// // click(){
// // createAddWindow();
// // }
// // },
// // {
// // label: 'Clear Item',
// // click(){
// // let del = knex('Items').del();
// // del.then((rows)=>{
// // console.log('deleted');
// // })
// // mainWindow.webContents.send('item:clear');
// // }
// // },
// // {
// // label: 'Quit',
// // accelerator: process.platform == 'darwin' ? 'Command+Q' : 'Ctrl+Q',
// // click(){
// // app.quit();
// // }
// // }
// ]
}
]
// if mac push an extra empty object in the beginning
if (process.platform == 'darwin') {
mainMenuTemplate.unshift({});
}
// show developer tools
if (process.env.NODE_ENV != 'production') {
mainMenuTemplate.push({
label: 'Developer Tools',
submenu: [
{
label: 'Toggle DevTools',
accelerator: process.platform == 'darwin' ? 'Command+I' : 'Ctrl+I',
click(item, focusedWindow) {
focusedWindow.toggleDevTools();
}
},
{
role: 'reload'
}
]
})
}
mainMenuTemplate.push({
label: 'File',
submenu: [
{
label: 'Log out',
// accelerator: process.platform == 'darwin' ? 'Command+I' : 'Ctrl+I',
click(item, focusedWindow) {
console.log(focusedWindow);
logout();
}
},
]
})