-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,106 additions
and
1,143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,128 @@ | ||
|
||
var bcrypt = require("bcrypt"); | ||
const bcrypt = require("bcrypt"); | ||
|
||
function AccountUtils() { | ||
|
||
} | ||
|
||
var accountUtils = new AccountUtils(); | ||
|
||
module.exports = { | ||
accountUtils: accountUtils | ||
}; | ||
|
||
var dbUtils = require("./dbUtils").dbUtils; | ||
|
||
AccountUtils.prototype.generatePasswordHash = function(password, done) { | ||
bcrypt.hash(password, 10, function(error, result) { | ||
if (error) { | ||
done({ | ||
success: false, | ||
error: error | ||
}); | ||
return; | ||
} | ||
done({ | ||
success: true, | ||
hash: result | ||
}); | ||
}); | ||
} | ||
|
||
AccountUtils.prototype.comparePasswordWithHash = function(password, hash, done) { | ||
bcrypt.compare(password, hash, function(error, result) { | ||
if (error) { | ||
done({ | ||
success: false, | ||
error: error | ||
}); | ||
return; | ||
} | ||
done({ | ||
success: true, | ||
isMatch: result | ||
}); | ||
}); | ||
} | ||
|
||
AccountUtils.prototype.addAccount = function(account, done) { | ||
dbUtils.performQuery( | ||
"INSERT INTO Users (username, passwordHash, emailAddress, score) VALUES (?, ?, ?, 0)", | ||
[account.username, account.passwordHash, account.emailAddress], | ||
function (error, results, fields) { | ||
class AccountUtils { | ||
|
||
generatePasswordHash(password, done) { | ||
bcrypt.hash(password, 10, (error, result) => { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error)); | ||
done({ | ||
success: false, | ||
error: error, | ||
}); | ||
return; | ||
} | ||
done(null); | ||
} | ||
); | ||
} | ||
|
||
AccountUtils.prototype.getAccountByUsername = function(username, done) { | ||
dbUtils.performQuery( | ||
"SELECT * FROM Users WHERE username = ?", | ||
[username], | ||
function (error, results, fields) { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error), null); | ||
return; | ||
} | ||
if (results.length > 0) { | ||
done(null, results[0]); | ||
} else { | ||
done(null, null); | ||
} | ||
} | ||
); | ||
} | ||
|
||
AccountUtils.prototype.updateAccount = function(uid, valueSet, done) { | ||
var tempQueryTextList = []; | ||
var tempValueList = []; | ||
for (name in valueSet) { | ||
var tempValue = valueSet[name]; | ||
tempQueryTextList.push(name + " = ?"); | ||
tempValueList.push(tempValue); | ||
done({ | ||
success: true, | ||
hash: result, | ||
}); | ||
}); | ||
} | ||
var tempQueryText = tempQueryTextList.join(", "); | ||
tempValueList.push(uid); | ||
dbUtils.performQuery( | ||
"UPDATE Users SET " + tempQueryText + " WHERE uid = ?", | ||
tempValueList, | ||
function (error, results, fields) { | ||
|
||
comparePasswordWithHash(password, hash, done) { | ||
bcrypt.compare(password, hash, (error, result) => { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error)); | ||
done({ | ||
success: false, | ||
error: error, | ||
}); | ||
return; | ||
} | ||
done(null); | ||
done({ | ||
success: true, | ||
isMatch: result, | ||
}); | ||
}); | ||
} | ||
|
||
addAccount(account, done) { | ||
dbUtils.performQuery( | ||
"INSERT INTO Users (username, passwordHash, emailAddress, score) VALUES (?, ?, ?, 0)", | ||
[account.username, account.passwordHash, account.emailAddress], | ||
(error, results, fields) => { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error)); | ||
return; | ||
} | ||
done(null); | ||
}, | ||
); | ||
} | ||
|
||
getAccountByUsername(username, done) { | ||
dbUtils.performQuery( | ||
"SELECT * FROM Users WHERE username = ?", | ||
[username], | ||
(error, results, fields) => { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error), null); | ||
return; | ||
} | ||
if (results.length > 0) { | ||
done(null, results[0]); | ||
} else { | ||
done(null, null); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
updateAccount(uid, valueSet, done) { | ||
const tempQueryTextList = []; | ||
const tempValueList = []; | ||
for (const name in valueSet) { | ||
var tempValue = valueSet[name]; | ||
tempQueryTextList.push(name + " = ?"); | ||
tempValueList.push(tempValue); | ||
} | ||
); | ||
const tempQueryText = tempQueryTextList.join(", "); | ||
tempValueList.push(uid); | ||
dbUtils.performQuery( | ||
"UPDATE Users SET " + tempQueryText + " WHERE uid = ?", | ||
tempValueList, | ||
(error, results, fields) => { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error)); | ||
return; | ||
} | ||
done(null); | ||
}, | ||
); | ||
} | ||
|
||
removeAccount(uid, done) { | ||
dbUtils.performQuery( | ||
"DELETE FROM Users WHERE uid = ?", | ||
[uid], | ||
(error, results, fields) => { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error)); | ||
return; | ||
} | ||
done(null); | ||
}, | ||
); | ||
} | ||
|
||
getLeaderboardAccounts(amount, done) { | ||
dbUtils.performQuery( | ||
"SELECT * FROM Users ORDER BY score DESC LIMIT 20", | ||
[], | ||
(error, results, fields) => { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error), null); | ||
return; | ||
} | ||
done(null, results); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
AccountUtils.prototype.removeAccount = function(uid, done) { | ||
dbUtils.performQuery( | ||
"DELETE FROM Users WHERE uid = ?", | ||
[uid], | ||
function (error, results, fields) { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error)); | ||
return; | ||
} | ||
done(null); | ||
} | ||
); | ||
} | ||
const accountUtils = new AccountUtils(); | ||
|
||
AccountUtils.prototype.getLeaderboardAccounts = function(amount, done) { | ||
dbUtils.performQuery( | ||
"SELECT * FROM Users ORDER BY score DESC LIMIT 20", | ||
[], | ||
function (error, results, fields) { | ||
if (error) { | ||
done(dbUtils.convertSqlErrorToText(error), null); | ||
return; | ||
} | ||
done(null, results); | ||
} | ||
); | ||
} | ||
module.exports = { accountUtils }; | ||
|
||
const { dbUtils } = require("./dbUtils"); | ||
|
||
|
Oops, something went wrong.