-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.class.js
159 lines (154 loc) · 6.89 KB
/
users.class.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
let pool = require("./mysql.class.js").Connect()
let bcrypt = require("bcrypt")
let thisclass = {
Login: function(username, password) {
return new Promise(function(resolve, reject) {
// Ensure username & password are pressent.
if(username, password) {
// Get a connection from the mysql connection pool.
pool.getConnection((err, connection) => {
// if no error from retrieving connection.
if(!err) {
// get the user id & password hash from the db. Limit this by one as username needs to be unique.
connection.query("SELECT id,password FROM users WHERE username = ? LIMIT 1", [username], (err, rows) => {
// Ensure user is pressent based on returned row count.
if(rows.length > 0) {
// Lets check that the plain password from user matches the users hashed password in the db.
bcrypt.compare(password, rows[0].password, function(err, result) {
if(!err) {
// store this as a let, as we will require it shortly.
let userid = rows[0].id;
// If password matches hash, begin login actions.
if(result == true) {
connection.query("UPDATE users SET lastloggedin = CURRENT_TIMESTAMP WHERE id = ?", [rows[0].id], (err, rows) => {
if(!err) {
if(rows.affectedRows > 0) {
// Release connection back to pool.
connection.release();
resolve({"Result":1, "Message":"User has authenticated successfully.", "UserID":userid})
} else {
// Release connection back to pool.
connection.release();
reject({"Result":0, "Message":"Sorry, we couldnt log you in. [mysql]"})
}
} else {
// Release connection back to pool.
connection.release();
reject({"Result":0, "Message":"Sorry, we couldnt log you in. [mysql]"})
}
})
} else {
// Release connection back to pool.
connection.release();
reject({"Result":0, "Message":"Sorry, that password does not match our records."})
}
} else {
// Release connection back to pool.
connection.release();
console.log(err)
reject({"Result":0, "Message":"Sorry, something went wrong with the server. [hash]"})
}
})
} else {
// Release connection back to pool.
connection.release();
reject({"Result":0, "Message":"Sorry, we can't find an account with that username?"})
}
})
} else {
console.log(err);
reject({"Result":0, "Message":"Sorry, something went wrong with the server. [mysql]"})
}
})
} else {
reject({"Result":0, "Message":"Please ensure all data is pressent."})
}
});
},
Register: function(username, password, firstname, lastname) {
return new Promise(function(resolve, reject) {
// Ensure data is pressent.
if(username, password, firstname, lastname) {
pool.getConnection((err, connection) => {
if(!err) {
// Firstly, lets check that the username is available.
connection.query("SELECT COUNT(id) Count FROM users WHERE username = ?", [username], (err, rows) => {
if(!err) {
if(rows[0].Count == 0) {
// Hash the password.
bcrypt.hash(password, 12, function(err, hash) {
if(!err) {
// insert user into database.
connection.query("INSERT INTO users (username, password, firstname, lastname) VALUES (?, ?, ?, ?)", [username, hash, firstname, lastname], (err, rows) => {
if(!err) {
// User has successfully been added to db.
connection.release()
resolve({"Result":1, "Message":"Your account has been created. Please login."})
} else {
connection.release()
console.log(err)
reject({"Result":0, "Message":"Sorry, something went wrong there. [mysql]"})
}
})
} else {
connection.release();
console.log(err)
reject({"Result":0, "Message":"Sorry, something went wrong there. [hash]"})
}
});
} else {
// Already Exists.
connection.release();
reject({"Result":0, "Message":"Sorry, that username has been taken."})
}
} else {
connection.release();
console.log(err)
reject({"Result":0, "Message":"Sorry, something went wrong there. [mysql]"})
}
})
} else {
connection.release();
console.log(err)
reject({"Result":0, "Message":"Sorry, something went wrong there. [mysql]"})
}
})
} else {
reject({"Result":0, "Message":"Please ensure all data is pressent."})
}
});
},
Info: function(id) {
return new Promise(function(resolve, reject) {
if(id) {
pool.getConnection((err, connection) => {
if(!err) {
// run query to fetch user information based on users id.
connection.query("SELECT username,firstname,lastname,lastloggedin FROM users WHERE id = ? LIMIT 1", id, (err, rows) => {
if(!err) {
if(rows.length > 0) {
connection.release();
resolve({"Result":1, "Message":"Cookie valid, found information.", "Data":rows[0]})
} else {
connection.release();
reject({"Result":0, "Message":"Unable to obtain user information."})
}
} else {
connection.release();
console.log(err)
reject({"Result":0, "Message":"Sorry, something went wrong. [mysql]"})
}
})
} else {
connection.release();
console.log(err)
reject({"Result":0, "Message":"Sorry, something went wrong. [mysql]"})
}
})
} else {
reject({"Result":0, "Message":"Please ensure all data is pressent."})
}
});
}
}
module.exports = thisclass;