1
- " use strict" ;
1
+ ' use strict' ;
2
2
3
3
/*****************************************************************************
4
4
* This is the JavaScript file that students need to modify to implement the
14
14
*
15
15
* Most of the functions in this file handle a form submission. These
16
16
* are passed as arguments the input/output DOM elements of the form that was
17
- * submitted. The " this" keyword for these functions is the form element
17
+ * submitted. The ' this' keyword for these functions is the form element
18
18
* itself. The functions that handle form submissions are:
19
19
* - login
20
20
* - signup
@@ -92,8 +92,8 @@ async function credentials(username) {
92
92
var idResult ;
93
93
94
94
// get any information needed to log in
95
- idResult = await serverRequest ( " identify" , {
96
- " username" : username
95
+ idResult = await serverRequest ( ' identify' , {
96
+ ' username' : username
97
97
} ) ;
98
98
// bail if something went wrong
99
99
if ( ! idResult . response . ok ) {
@@ -124,9 +124,9 @@ async function login(userInput, passInput) {
124
124
hashedPassword = await hashMessage ( hashedPassword + idJson [ 'challenge' ] ) ;
125
125
126
126
// Send a login request to the server.
127
- const result = await serverRequest ( " login" , {
128
- " username" : username ,
129
- " password" : hashedPassword
127
+ const result = await serverRequest ( ' login' , {
128
+ ' username' : username ,
129
+ ' password' : hashedPassword
130
130
} ) ;
131
131
132
132
// If the login was successful, show the dashboard.
@@ -136,7 +136,7 @@ async function login(userInput, passInput) {
136
136
sessionStorage . setItem ( 'encryption_key' , encryptionKey ) ;
137
137
clearInputs ( ) ;
138
138
139
- showContent ( " dashboard" ) ;
139
+ showContent ( ' dashboard' ) ;
140
140
} else {
141
141
// If the login failed, show the login page with an error message.
142
142
serverStatus ( result ) ;
@@ -158,16 +158,16 @@ async function signup(userInput, passInput, passInput2, emailInput) {
158
158
password = await hashMessage ( username + password ) ;
159
159
160
160
// send the signup form to the server
161
- const result = await serverRequest ( " signup" , {
162
- " username" : username ,
163
- " password" : password ,
164
- " email" : email
161
+ const result = await serverRequest ( ' signup' , {
162
+ ' username' : username ,
163
+ ' password' : password ,
164
+ ' email' : email
165
165
} ) ;
166
166
167
167
if ( result . response . ok ) {
168
168
clearInputs ( ) ;
169
169
// go to the login page
170
- showContent ( " login" ) ;
170
+ showContent ( ' login' ) ;
171
171
}
172
172
// show the status message from the server
173
173
serverStatus ( result ) ;
@@ -190,19 +190,19 @@ async function save(siteInput, userInput, passInput) {
190
190
} = await encryptMessage ( sitepasswd , key ) ;
191
191
192
192
// send the data, along with the encrypted password, to the server
193
- const result = await serverRequest ( " save" , {
194
- " site" : site ,
195
- " siteuser" : siteuser ,
196
- " sitepasswd" : encryptedMessage ,
197
- " siteiv" : iv
193
+ const result = await serverRequest ( ' save' , {
194
+ ' site' : site ,
195
+ ' siteuser' : siteuser ,
196
+ ' sitepasswd' : encryptedMessage ,
197
+ ' siteiv' : iv
198
198
} ) ;
199
199
200
200
if ( result . response . ok ) {
201
201
// any work after a successful save should be done here
202
202
clearInputs ( ) ;
203
203
204
204
// update the sites list
205
- sites ( " save" ) ;
205
+ sites ( ' save' ) ;
206
206
}
207
207
// show any server status messages
208
208
serverStatus ( result ) ;
@@ -217,8 +217,8 @@ async function save(siteInput, userInput, passInput) {
217
217
*/
218
218
async function loadSite ( siteName , siteElement , userElement , passElement ) {
219
219
const isDecryptionRequired = passElement . tagName === 'INPUT' ;
220
- const result = await serverRequest ( " load" , {
221
- " site" : siteName
220
+ const result = await serverRequest ( ' load' , {
221
+ ' site' : siteName
222
222
} ) ;
223
223
224
224
if ( result . response . ok ) {
@@ -238,7 +238,7 @@ async function loadSite(siteName, siteElement, userElement, passElement) {
238
238
}
239
239
} else {
240
240
// on failure, show the login page and display any server status
241
- showContent ( " login" ) ;
241
+ showContent ( ' login' ) ;
242
242
serverStatus ( result ) ;
243
243
}
244
244
}
@@ -264,15 +264,15 @@ async function load(siteInput, userInput, passInput) {
264
264
* Called when the logout link is clicked.
265
265
*/
266
266
async function logout ( ) {
267
- const result = await serverRequest ( " logout" , { } ) ;
267
+ const result = await serverRequest ( ' logout' , { } ) ;
268
268
269
269
if ( result . response . ok ) {
270
270
sessionStorage . removeItem ( 'encryption_key' ) ;
271
271
sessionStorage . removeItem ( 'hexiv' ) ;
272
272
273
273
clearInputs ( ) ;
274
274
275
- showContent ( " login" ) ;
275
+ showContent ( ' login' ) ;
276
276
}
277
277
278
278
serverStatus ( result ) ;
@@ -282,78 +282,78 @@ async function logout() {
282
282
* Called when the value in password2 is changed.
283
283
*/
284
284
function validatePasswordsMatching ( ) {
285
- const password = document . querySelector ( " #signup form .field input[name=password]" ) ;
286
- const password2 = document . querySelector ( " #signup form .field input[name=password2]" ) ;
285
+ const password = document . querySelector ( ' #signup form .field input[name=password]' ) ;
286
+ const password2 = document . querySelector ( ' #signup form .field input[name=password2]' ) ;
287
287
288
- if ( password2 . value === "" ) {
289
- password2 . setCustomValidity ( " Please confirm password!" ) ;
288
+ if ( password2 . value === '' ) {
289
+ password2 . setCustomValidity ( ' Please confirm password!' ) ;
290
290
} else if ( password . value !== password2 . value ) {
291
- password2 . setCustomValidity ( " Passwords do not match!" ) ;
291
+ password2 . setCustomValidity ( ' Passwords do not match!' ) ;
292
292
} else {
293
- password2 . setCustomValidity ( "" ) ;
293
+ password2 . setCustomValidity ( '' ) ;
294
294
}
295
295
}
296
296
297
297
/**
298
298
* Called when the value in email is changed.
299
299
*/
300
300
function validateEmail ( ) {
301
- const email = document . querySelector ( " #signup form .field input[name=email]" ) ;
302
- const emailRegex = new RegExp ( / ^ ( ( [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ( \. [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ) * ) | ( " . + " ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ / ) ;
301
+ const email = document . querySelector ( ' #signup form .field input[name=email]' ) ;
302
+ const emailRegex = new RegExp ( / ^ ( ( [ ^ < > ( ) \[ \] \\ . , ; : \s @ ' ] + ( \. [ ^ < > ( ) \[ \] \\ . , ; : \s @ ' ] + ) * ) | ( ' . + ' ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ / ) ;
303
303
304
- if ( email . value === "" ) {
305
- email . setCustomValidity ( " Please enter email!" ) ;
304
+ if ( email . value === '' ) {
305
+ email . setCustomValidity ( ' Please enter email!' ) ;
306
306
} else if ( ! emailRegex . test ( email . value ) ) {
307
- email . setCustomValidity ( " Must be a valid email address!" ) ;
307
+ email . setCustomValidity ( ' Must be a valid email address!' ) ;
308
308
} else {
309
- email . setCustomValidity ( "" ) ;
309
+ email . setCustomValidity ( '' ) ;
310
310
}
311
311
}
312
312
313
313
/**
314
314
* Called when the sign up submit button is clicked.
315
315
*/
316
316
function validateSignupInfo ( ) {
317
- const username = document . querySelector ( " #signup form .field input[name=username]" ) ;
318
- const password = document . querySelector ( " #signup form .field input[name=password]" ) ;
319
- const password2 = document . querySelector ( " #signup form .field input[name=password2]" ) ;
320
- const email = document . querySelector ( " #signup form .field input[name=email]" ) ;
317
+ const username = document . querySelector ( ' #signup form .field input[name=username]' ) ;
318
+ const password = document . querySelector ( ' #signup form .field input[name=password]' ) ;
319
+ const password2 = document . querySelector ( ' #signup form .field input[name=password2]' ) ;
320
+ const email = document . querySelector ( ' #signup form .field input[name=email]' ) ;
321
321
const usernameRegex = new RegExp ( / ^ [ a - z A - Z 0 - 9 ] [ a - z A - Z 0 - 9 - _ ] { 2 , 20 } $ / ) ;
322
322
const passwordRegex = new RegExp ( / (? = .* \d ) (? = .* [ a - z ] ) (? = .* [ A - Z ] ) .{ 6 , 16 } / ) ;
323
- const emailRegex = new RegExp ( / ^ ( ( [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ( \. [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ) * ) | ( " . + " ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ / ) ;
323
+ const emailRegex = new RegExp ( / ^ ( ( [ ^ < > ( ) \[ \] \\ . , ; : \s @ ' ] + ( \. [ ^ < > ( ) \[ \] \\ . , ; : \s @ ' ] + ) * ) | ( ' . + ' ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ / ) ;
324
324
325
325
if ( ! usernameRegex . test ( username . value ) ) {
326
- console . log ( " Username is invalid! You can use letters, numbers, - and _. Must start with a letter or a number, and at least 3 and at most 20 characters." ) ;
326
+ console . log ( ' Username is invalid! You can use letters, numbers, - and _. Must start with a letter or a number, and at least 3 and at most 20 characters.' ) ;
327
327
328
328
return false ;
329
329
}
330
330
331
331
if ( ! passwordRegex . test ( password . value ) ) {
332
- console . log ( " Password is invalid! Must contain at least one number, one uppercase and one lowercase letter, and at least 6 and at most 16 characters." ) ;
332
+ console . log ( ' Password is invalid! Must contain at least one number, one uppercase and one lowercase letter, and at least 6 and at most 16 characters.' ) ;
333
333
334
334
return false ;
335
335
}
336
336
337
- if ( password2 . value === "" ) {
338
- console . log ( " Please confirm password!" ) ;
337
+ if ( password2 . value === '' ) {
338
+ console . log ( ' Please confirm password!' ) ;
339
339
340
340
return false ;
341
341
}
342
342
343
343
if ( password . value !== password2 . value ) {
344
- console . log ( " Passwords do not match!" ) ;
344
+ console . log ( ' Passwords do not match!' ) ;
345
345
346
346
return false ;
347
347
}
348
348
349
- if ( email . value === "" ) {
350
- console . log ( " Please enter email!" ) ;
349
+ if ( email . value === '' ) {
350
+ console . log ( ' Please enter email!' ) ;
351
351
352
352
return false ;
353
353
}
354
354
355
355
if ( ! emailRegex . test ( email . value ) ) {
356
- console . log ( " Must be a valid email address!" ) ;
356
+ console . log ( ' Must be a valid email address!' ) ;
357
357
358
358
return false ;
359
359
}
0 commit comments