-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (83 loc) · 2.1 KB
/
index.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
const {v4} = require('uuid')
// Mongo
const database = {
users: [
{
uuid: '3ceded28-1562-4b47-8e9b-8691bb1fde93',
email: '[email protected]',
password: '12345', // do not do this in production
sessions: [
{
token: 'asdasd',
startedAt: new Date(),
endedAt: null, // TODO: implement this
active: false // TODO: implement this
}
]
}
]
}
class LoginSystem {
constructor({email, password}) {
this.email = email
this.password = password
this.uuid = v4()
this.sessions = []
}
login() {
if (!this.email) {
return {error: true, message: 'invalid email'}
}
if (!this.password) {
return {error: true, message: 'invalid password'}
}
const existingUser = database.users.find(user => user.email === this.email)
if (!existingUser) {
return {error: true, message: 'user not found'}
}
if (existingUser.password !== this.password) {
return {error: true, message: 'password does not match'}
}
this.createNewSession()
}
logout() {
this.email = ''
this.password = ''
this.uuid = ''
this.sessions = []
}
changePassword(newPassword) {
this.password = newPassword
database.users.map(user => {
if (user.email === this.email) {
user.password = newPassword
return user
}
})
}
getUserSession() {
if (!this.email) {
return {error: true, message: 'invalid email'}
}
const _user = database.users.find(user => user.email === this.email)
return _user.sessions
}
createNewSession() {
database.users.map(user => {
if (user.email === this.email) {
user.sessions.push({token: '12345', startedAt: new Date()})
return user
}
})
}
}
const loginSystem = new LoginSystem({
email: '[email protected]',
password: '12345'
})
loginSystem.login()
const userSessions = loginSystem.getUserSession()
console.log('userSessions: ', userSessions)
loginSystem.changePassword('54321')
console.log('password: ', loginSystem.password)
module.exports = {LoginSystem}