-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain-menu.js
129 lines (117 loc) · 3.35 KB
/
main-menu.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
$(document).ready(async function() {
appStartUp();
});
async function appStartUp() {
// Disable buttons while checking.
const buttons = document.getElementsByClassName('btn');
for (let i = 0; i < buttons.length; i++) {
buttons[i].setAttribute("disabled", "");
}
// Check if user database exists.
if (fs.existsSync(path.join(__dirname, 'db', 'user.json'))) {
// Check if there's actually data in it.
const userDbLength = await getDbTableSize('user');
if (userDbLength >= 1) {
// Check if a user has logged in yet.
if (localStorage.getItem('lastLoginUserID') != null) {
await mainMenuStartUp();
await setWelcomeMsgName();
// Re-enable buttons.
for (let i = 0; i < buttons.length; i++) {
buttons[i].removeAttribute("disabled");
}
} else {
changePage('login.html');
}
} else {
changePage('login.html');
}
} else {
// Fresh install, go to login and set up files.
setupDbFiles();
changePage('login.html');
}
}
function setupDbFiles() {
// Check if database folder exists yet, and create if not.
if (!fs.existsSync(path.join(__dirname, 'db'))) {
fs.mkdirSync(path.join(__dirname, 'db'));
}
// Create new db files.
createNewDbTable('device');
createNewDbTable('user');
}
function createNewDbTable(tableName) {
const location = path.join(__dirname, 'db');
db.createTable(tableName, location, (succ, msg) => {
if (succ) {
log.error(msg + 'JSON db created.');
} else {
log.error('An error has occured. ' + msg);
}
});
}
async function mainMenuStartUp() {
// Check which user was last logged in.
lastLoggedInUserID = parseInt(localStorage.getItem('lastLoginUserID'));
// Get db data.
const currentUserObj = {userId: lastLoggedInUserID},
userCreds = await getDbRowWhere('user', currentUserObj);
return new Promise((resolve, reject) => {
// Check if a session token was passed from the previous page.
sessionToken = window.location.hash.substring(1);
if (sessionToken == null || sessionToken == "" || sessionToken == "undefined") {
// If none found, generate new one.
// Get the user's credentials from db, using the user ID.
const emailAdd = userCreds[0].email,
password = userCreds[0].password;
// Generate a session token for this user with the API.
var apiRequest = {
"url": "https://my.farmbot.io/api/tokens",
"method": "POST",
"timeout": 0,
"headers": {
"content-type": "application/json"
},
"data": JSON.stringify({
"user": {
"email": emailAdd,
"password": password
}
}),
};
$.ajax(apiRequest)
.done(function (response) {
sessionToken = response.token.encoded;
log.info("Session token generated: " + sessionToken);
apiConnected = true;
}).then(function(response){
resolve(response);
});
} else {
setWelcomeMsgName();
resolve();
}
});
}
function setWelcomeMsgName() {
return new Promise((resolve, reject) => {
var apiRequest = {
"url": "https://my.farmbot.io/api/users",
"method": "GET",
"timeout": 0,
"headers": {
"Authorization": "Bearer " + sessionToken,
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
},
};
$.ajax(apiRequest)
.done(function (response) {
document.getElementById("welcome-msg").innerHTML = "Hi, " + response[0].name;
apiConnected = true;
}).then(function(response){
resolve(response);
});
});
}