-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
88 lines (76 loc) · 2.02 KB
/
test.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
'use strict';
// This class is used for logins
class Login {
constructor(hash) {
this.sessions = [];
this.users = [];
this.passwords = [];
Object.keys(hash).map(k => ({k, v: hash[k]})).map(e => {
this.users = this.users.concat([e.k]);
this.passwords = this.passwords.concat([e.v]);
});
}
logout(user) {
for (let i=0; i<this.sessions.length; i++) {
if(this.sessions[i]===user){
this.sessions[i] = null;
}
}
this.sessions = this.sessions.filter(session => session !== null);
}
// Checks if user exists
userExists(user) {
for (let i of this.users) {
if (i === user) {
return true;
}
}
return false;
}
// Register user
registerUser(user, password) {
this.users.push(user);
this.passwords.push(password);
}
removeUser(user) {
let index = this.users.indexOf(user);
this.users[index] = null;
this.passwords[index] = null;
this.users = this.users.filter(user => user !== null);
this.passwords = this.passwords.filter(password => password !== null);
}
checkPassword(user, password) {
let index = this.users.indexOf(user);
let passwordCorrect = this.passwords[index] === password;
return passwordCorrect;
}
updatePassword(user, oldPassword, newPassword) {
// First we check if the user exists
if(this.userExists(user)){
let index = this.users.indexOf(user);
if (this.passwords[index] === oldPassword) {
this.passwords[index] = newPassword;
return true;
}
}
return false;
}
login(user, password) {
let index = this.users.indexOf(user);
if (this.passwords[index] === password) {
this.sessions.push(user);
}
}
}
let registeredUsers = {
user1: 'pass1',
user2: 'pass2',
user3: 'pass3'
};
let login = new Login(registeredUsers);
login.registerUser('user4', 'pass4');
login.login('user4', 'pass4');
login.updatePassword('user3', 'pass3', 'pass5');
login.login('user3', 'pass5');
login.logout('user4');
login.logout('user3');