Repository navigation
Expand file tree
/
Copy pathmainWindow.js
More file actions
139 lines (124 loc) · 3.5 KB
/
Copy pathmainWindow.js
File metadata and controls
139 lines (124 loc) · 3.5 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
// Modules
const {BrowserWindow, ipcMain} = require('electron');
const datastore = require('./datastore.js');
const filewatch = require('./datasource/filewatch');
//Node Emailer variables
const nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'horus.sustaingineering@gmail.com',
pass: 'horus4ever!'
}
});
var loadingState = "Kicking off engines."
//User Email
var userEmail = ""
//Verification Code
var verificationCode = ""
exports.win
// mainWindow createWindow fn
exports.createWindow = (mainWindow, fileName) => {
this.win = mainWindow
// Create full size app
//this.win.maximize();
if (fileName ) { this.win.loadURL(`file://${__dirname}/renderer/${fileName}`) }
else { this.win.loadURL(`file://${__dirname}/renderer/main.html`) }
// Devtools
//this.win.webContents.openDevTools()
this.win.once('ready-to-show', async () => {
this.win.show()
loadingState = "Initializing Local Datastore.."
loadingState = "Local Datastore initialized.."
return true;
// updateLoadingState('Initializing datastore')
})
// Handle window closed
this.win.on('closed', () => {
this.win = null
})
return this.win;
}
var loadPage = exports.loadPage = (name) => {
if (name === 'monitor.html') {
this.win.loadURL('file://' + __dirname + '/renderer/monitor.html')
} else{
this.win.loadURL(`file://${__dirname}/renderer/${name}`)
}
}
// [ Triggers ]
ipcMain.on('loading-state', (e, msg) => {
e.sender.send('loading-state', loadingState)
})
ipcMain.on('is-new-user', async (e, msg) => {
try {
let isOldUser = await datastore.findUser(msg.email)
if (isOldUser) {
e.sender.send('is-new-user', {error:"User already exists"})
return
}
await datastore.newUser(msg)
loadPage('login.html')
} catch(error) {
e.sender.send('is-new-user', false)
}
})
//Verify email
ipcMain.on('email-exists', async (e, msg) => {
try {
let emailExists = await datastore.findUser(msg.email)
if (!emailExists) {
e.sender.send('email-exists', {error:"Email does not exist"})
return
}
userEmail = msg.email
let randomCode = Math.random().toString(36).substring(7);
const mailOptions = {
from: 'horus.sustaingineering@gmail.com',
to: userEmail,
subject: 'Reset your Password Verification Code',
html: `
<p>
Hi,
This is your temporary verification code: ` + randomCode + `
</p>
`,
};
verificationCode = randomCode
await transporter.sendMail(mailOptions, function (error, info) {
if(error) {
console.log(error);
} else {
console.log(info);
}
});
loadPage('verifyCode.html')
} catch(error) {
e.sender.send('email-exists', false)
}
})
//Reset Password
ipcMain.on('new-password', async (e, msg) => {
try {
//Save new password to the mapped email
await datastore.newPassword({email: userEmail, password: msg.password})
e.sender.send('new-password', {success: 'Password successfully changed!'})
loadPage('login.html')
} catch(error) {
e.sender.send('new-password', false)
}
})
//VerifyCode
ipcMain.on('verify-code', async (e, msg) => {
try {
//Save new password to the mapped email
if(!(msg.code === verificationCode)) {
e.sender.send('verify-code', {error: 'Wrong Verification Code entered'})
return
}
verificationCode = ""
loadPage('resetPassword.html')
} catch(error) {
e.sender.send('verify-code', false)
}
})