-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontEndController.js
417 lines (361 loc) · 11.7 KB
/
frontEndController.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// @ts-check
import jwt from 'jsonwebtoken'
/**
* This is the Frontend Controller of WebNotes
* @category Controller
*/
export class FrontEndController {
static userTokenName = "webnotes.auth.token";
static currentNoteName = "webnotes.noteID";
//#region User Methods
/**
* This method checks whether a given user exists in the database
* @param {string} username name to check
* @returns {Promise<boolean>} True if user exists, else false
*/
static async doesUserExist(username) {
const response = await fetch('./api/users/does_exist', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: username
})
});
const data = await response.json();
return data.wasSuccessfull;
}
/**
* This method is used to store the id of the current note in the local storage
* @param {number} noteID ID of the note to be set as the current note
*/
static setCurrentNoteID(noteID) {
localStorage.removeItem(FrontEndController.currentNoteName);
localStorage.setItem(FrontEndController.currentNoteName, String(noteID));
}
/**
* This method is used to get the id of the current note from the local storage
* @returns {number} ID of the current note
*/
static getCurrentNoteID() {
const currentNoteString = localStorage.getItem(FrontEndController.currentNoteName);
if (currentNoteString === null) {
return undefined;
}
return Number(currentNoteString);
}
/**
* This method removes the note id from the local storage
*/
static removeCurrentNoteID() {
localStorage.removeItem(FrontEndController.currentNoteName);
}
/**
* This method returns a filled User object for the given user.
* @param {string} token Token of the user
* @returns {Promise<{id: number, name: string, password: string}>} User object with all credentials of the user from the token, empty User if token not valid
*/
static async getUserFromToken(token) {
const response = await fetch('./api/users/get_user', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: token,
})
});
const data = await response.json();
return data.user[0];
}
/**
* This method checks the password for current password requirements
* @param {string} password password to validate with requirements
* @returns {Promise<boolean>} True if requirements are met, false if not
*/
static async isPasswordValid(password) {
const response = await fetch('./api/users/is_password_valid', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
password: password
})
});
const data = await response.json();
return data.wasSuccessfull;
}
/**
* This method checks the username for current username requirements
* @param {string} username username to validate with requirements
* @returns {Promise<boolean>} True if requirements are met, false if not
*/
static async isUsernameValid(username) {
const response = await fetch('./api/users/is_username_valid', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: username
})
});
const data = await response.json();
return data.wasSuccessfull;
}
/**
* This method logs a user in if there is a match with the database. Therfore a token is created which is stored in the browsers local storage.
* @param {string} username Username to log in
* @param {string} password Password for user
* @returns {Promise<boolean>} True if login was successfull, false if not
*/
static async loginUser(username, password) {
const response = await fetch('./api/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: username,
password: password,
})
});
const data = await response.json();
if (data.userToken === "") {
localStorage.removeItem(this.userTokenName);
return false;
}
localStorage.setItem(this.userTokenName, data.userToken);
return true;
}
/**
* This method registers a user to the database
* @param {string} username the username of the user to be created
* @param {string} password the password of the user to be created
* @returns {Promise<boolean>} true if registration was successfull, false if not
*/
static async registerUser(username, password) {
const response = await fetch('./api/users/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: username,
password: password,
})
});
const data = await response.json();
if (data.wasSuccessfull) {
await FrontEndController.loginUser(username, password);
}
return data.wasSuccessfull;
}
/**
* This method changes the password of the current user
* @param {string} oldPassword the old password of the user
* @param {string} newPassword the new password of the user
* @returns {Promise<boolean>} True if password was changed, false if not
*/
static async changePassword(oldPassword, newPassword) {
const response = await fetch('./api/users/change_password', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userToken: this.getUserToken(),
oldPassword: oldPassword,
newPassword: newPassword
})
});
const data = await response.json();
if (data.wasSuccessfull) {
return await this.loginUser(this.getUsernameFromToken(this.getUserToken()), newPassword);
}
return false;
}
/**
* This method checks whether the given token has a valid signature and user
* @param {string} token token to be verified
* @returns {Promise<boolean>} true if signature is valid and user exists, false if not
*/
static async verifyUserByToken(token) {
const response = await fetch('./api/users/verify_token', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: token,
})
});
const data = await response.json();
return data.wasSuccessfull;
}
/**
* This mehtod loggs out the current user.
* @returns {boolean} True if logout was successfull, false if not
*/
static logoutUser() {
localStorage.removeItem(this.userTokenName);
return true;
}
/**
* This method gets all the users from the database
* @returns {Promise<Array.<{id: number, name: string}>>} Array of all users
*/
static async getAllUsers() {
const response = await fetch('./api/users/get_all_users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userToken: FrontEndController.getUserToken()
})
});
const data = await response.json();
return data.users;
}
//#endregion
//#region Token Methods
/**
* This method returns the current authentication token
* @returns {string} token of the currently logged in user
*/
static getUserToken() {
const token = localStorage.getItem(this.userTokenName);
if (token !== null) {
return token;
}
return "";
}
/**
* This method extracts the username from the token and returns it.
* @param {string} token Token with user information
* @returns {string} Username if token contains username, else empty string
*/
static getUsernameFromToken(token) {
const content = jwt.decode(token);
if (typeof content === "object" && content !== null) {
return content.username;
}
return "";
}
//#endregion
//#region Note Methods
/**
* This method is used to save a note to the database. If no note.id is given, a new note is created.
* @param {{id: number | undefined, title: string | undefined, content: string | undefined, inUse: string | undefined, isShared: boolean | undefined, sharedUserIDs: number[] | undefined}} note note which should be saved
* @returns {Promise<number>} returns the id of the saved note
*/
static async saveNote(note) {
const response = await fetch('./api/notes/save_note', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
note: note,
userToken: FrontEndController.getUserToken()
})
});
const data = await response.json();
return data.noteID;
}
/**
* This function writes the username in the inUse field of the note to make it read only for other users.
* @param {number} noteID ID of the note which should be made read only
*/
static async setNoteInUse(noteID) {
this.saveNote({
id: noteID,
inUse: this.getUsernameFromToken(this.getUserToken()),
title: undefined,
content: undefined,
isShared: undefined,
sharedUserIDs: undefined
});
}
/**
* This function removes the username from the inUse field of a note to make it available for other users
* @param {number} noteID ID of the note which should be made available
*/
static async setNoteNotInUse(noteID) {
await this.saveNote({
id: noteID,
inUse: "",
title: undefined,
content: undefined,
isShared: undefined,
sharedUserIDs: undefined
});
}
/**
* This method is used to get all notes which are related to the user.
* @returns {Promise<Array.<{id: number, title: string, ownerID: number, modifiedAt: Date, content: string, inUse: string, isShared: boolean, sharedUserIDs: number[]}>>} Array of all notes of the current user
*/
static async getNotes() {
const response = await fetch('./api/notes/get_all_notes', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userToken: FrontEndController.getUserToken()
})
});
const data = await response.json();
return data.notes;
}
/**
* This method is used to get get a note by its id.
* @param {number} noteID id of the note
* @returns {Promise<{id: number, title: string, ownerID: number, modifiedAt: Date, content: string, inUse: string, isShared: boolean, sharedUserIDs: number[]}>} Array of all notes of the current user
*/
static async getNoteByID(noteID) {
const response = await fetch('./api/notes/get_note_by_id', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userToken: FrontEndController.getUserToken(),
id: noteID
})
});
const data = await response.json();
return data.note;
}
/**
* This method creates a new note.
* @returns {Promise<number>} ID of the created note
*/
static async addNewNote() {
const currentNote = { content: "", title: "Neue Notiz", id: undefined, inUse: this.getUsernameFromToken(this.getUserToken()), isShared: false, sharedUserIDs: [], }
const nodeID = await this.saveNote(currentNote);
return nodeID;
}
/**
* This method deletes the current note.
* @returns {Promise<boolean>} True if the note was deleted, false if not
*/
static async deleteNote() {
const response = await fetch('./api/notes/delete_note', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userToken: FrontEndController.getUserToken(),
id: this.getCurrentNoteID()
})
});
const data = await response.json();
return data.wasSuccessfull;
}
//#endregion
}