|
1 | 1 |
|
2 | | -var bcrypt = require("bcrypt"); |
| 2 | +const bcrypt = require("bcrypt"); |
3 | 3 |
|
4 | | -function AccountUtils() { |
5 | | - |
6 | | -} |
7 | | - |
8 | | -var accountUtils = new AccountUtils(); |
9 | | - |
10 | | -module.exports = { |
11 | | - accountUtils: accountUtils |
12 | | -}; |
13 | | - |
14 | | -var dbUtils = require("./dbUtils").dbUtils; |
15 | | - |
16 | | -AccountUtils.prototype.generatePasswordHash = function(password, done) { |
17 | | - bcrypt.hash(password, 10, function(error, result) { |
18 | | - if (error) { |
19 | | - done({ |
20 | | - success: false, |
21 | | - error: error |
22 | | - }); |
23 | | - return; |
24 | | - } |
25 | | - done({ |
26 | | - success: true, |
27 | | - hash: result |
28 | | - }); |
29 | | - }); |
30 | | -} |
31 | | - |
32 | | -AccountUtils.prototype.comparePasswordWithHash = function(password, hash, done) { |
33 | | - bcrypt.compare(password, hash, function(error, result) { |
34 | | - if (error) { |
35 | | - done({ |
36 | | - success: false, |
37 | | - error: error |
38 | | - }); |
39 | | - return; |
40 | | - } |
41 | | - done({ |
42 | | - success: true, |
43 | | - isMatch: result |
44 | | - }); |
45 | | - }); |
46 | | -} |
47 | | - |
48 | | -AccountUtils.prototype.addAccount = function(account, done) { |
49 | | - dbUtils.performQuery( |
50 | | - "INSERT INTO Users (username, passwordHash, emailAddress, score) VALUES (?, ?, ?, 0)", |
51 | | - [account.username, account.passwordHash, account.emailAddress], |
52 | | - function (error, results, fields) { |
| 4 | +class AccountUtils { |
| 5 | + |
| 6 | + generatePasswordHash(password, done) { |
| 7 | + bcrypt.hash(password, 10, (error, result) => { |
53 | 8 | if (error) { |
54 | | - done(dbUtils.convertSqlErrorToText(error)); |
| 9 | + done({ |
| 10 | + success: false, |
| 11 | + error: error, |
| 12 | + }); |
55 | 13 | return; |
56 | 14 | } |
57 | | - done(null); |
58 | | - } |
59 | | - ); |
60 | | -} |
61 | | - |
62 | | -AccountUtils.prototype.getAccountByUsername = function(username, done) { |
63 | | - dbUtils.performQuery( |
64 | | - "SELECT * FROM Users WHERE username = ?", |
65 | | - [username], |
66 | | - function (error, results, fields) { |
67 | | - if (error) { |
68 | | - done(dbUtils.convertSqlErrorToText(error), null); |
69 | | - return; |
70 | | - } |
71 | | - if (results.length > 0) { |
72 | | - done(null, results[0]); |
73 | | - } else { |
74 | | - done(null, null); |
75 | | - } |
76 | | - } |
77 | | - ); |
78 | | -} |
79 | | - |
80 | | -AccountUtils.prototype.updateAccount = function(uid, valueSet, done) { |
81 | | - var tempQueryTextList = []; |
82 | | - var tempValueList = []; |
83 | | - for (name in valueSet) { |
84 | | - var tempValue = valueSet[name]; |
85 | | - tempQueryTextList.push(name + " = ?"); |
86 | | - tempValueList.push(tempValue); |
| 15 | + done({ |
| 16 | + success: true, |
| 17 | + hash: result, |
| 18 | + }); |
| 19 | + }); |
87 | 20 | } |
88 | | - var tempQueryText = tempQueryTextList.join(", "); |
89 | | - tempValueList.push(uid); |
90 | | - dbUtils.performQuery( |
91 | | - "UPDATE Users SET " + tempQueryText + " WHERE uid = ?", |
92 | | - tempValueList, |
93 | | - function (error, results, fields) { |
| 21 | + |
| 22 | + comparePasswordWithHash(password, hash, done) { |
| 23 | + bcrypt.compare(password, hash, (error, result) => { |
94 | 24 | if (error) { |
95 | | - done(dbUtils.convertSqlErrorToText(error)); |
| 25 | + done({ |
| 26 | + success: false, |
| 27 | + error: error, |
| 28 | + }); |
96 | 29 | return; |
97 | 30 | } |
98 | | - done(null); |
| 31 | + done({ |
| 32 | + success: true, |
| 33 | + isMatch: result, |
| 34 | + }); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + addAccount(account, done) { |
| 39 | + dbUtils.performQuery( |
| 40 | + "INSERT INTO Users (username, passwordHash, emailAddress, score) VALUES (?, ?, ?, 0)", |
| 41 | + [account.username, account.passwordHash, account.emailAddress], |
| 42 | + (error, results, fields) => { |
| 43 | + if (error) { |
| 44 | + done(dbUtils.convertSqlErrorToText(error)); |
| 45 | + return; |
| 46 | + } |
| 47 | + done(null); |
| 48 | + }, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + getAccountByUsername(username, done) { |
| 53 | + dbUtils.performQuery( |
| 54 | + "SELECT * FROM Users WHERE username = ?", |
| 55 | + [username], |
| 56 | + (error, results, fields) => { |
| 57 | + if (error) { |
| 58 | + done(dbUtils.convertSqlErrorToText(error), null); |
| 59 | + return; |
| 60 | + } |
| 61 | + if (results.length > 0) { |
| 62 | + done(null, results[0]); |
| 63 | + } else { |
| 64 | + done(null, null); |
| 65 | + } |
| 66 | + }, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + updateAccount(uid, valueSet, done) { |
| 71 | + const tempQueryTextList = []; |
| 72 | + const tempValueList = []; |
| 73 | + for (const name in valueSet) { |
| 74 | + var tempValue = valueSet[name]; |
| 75 | + tempQueryTextList.push(name + " = ?"); |
| 76 | + tempValueList.push(tempValue); |
99 | 77 | } |
100 | | - ); |
| 78 | + const tempQueryText = tempQueryTextList.join(", "); |
| 79 | + tempValueList.push(uid); |
| 80 | + dbUtils.performQuery( |
| 81 | + "UPDATE Users SET " + tempQueryText + " WHERE uid = ?", |
| 82 | + tempValueList, |
| 83 | + (error, results, fields) => { |
| 84 | + if (error) { |
| 85 | + done(dbUtils.convertSqlErrorToText(error)); |
| 86 | + return; |
| 87 | + } |
| 88 | + done(null); |
| 89 | + }, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + removeAccount(uid, done) { |
| 94 | + dbUtils.performQuery( |
| 95 | + "DELETE FROM Users WHERE uid = ?", |
| 96 | + [uid], |
| 97 | + (error, results, fields) => { |
| 98 | + if (error) { |
| 99 | + done(dbUtils.convertSqlErrorToText(error)); |
| 100 | + return; |
| 101 | + } |
| 102 | + done(null); |
| 103 | + }, |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + getLeaderboardAccounts(amount, done) { |
| 108 | + dbUtils.performQuery( |
| 109 | + "SELECT * FROM Users ORDER BY score DESC LIMIT 20", |
| 110 | + [], |
| 111 | + (error, results, fields) => { |
| 112 | + if (error) { |
| 113 | + done(dbUtils.convertSqlErrorToText(error), null); |
| 114 | + return; |
| 115 | + } |
| 116 | + done(null, results); |
| 117 | + }, |
| 118 | + ); |
| 119 | + } |
101 | 120 | } |
102 | 121 |
|
103 | | -AccountUtils.prototype.removeAccount = function(uid, done) { |
104 | | - dbUtils.performQuery( |
105 | | - "DELETE FROM Users WHERE uid = ?", |
106 | | - [uid], |
107 | | - function (error, results, fields) { |
108 | | - if (error) { |
109 | | - done(dbUtils.convertSqlErrorToText(error)); |
110 | | - return; |
111 | | - } |
112 | | - done(null); |
113 | | - } |
114 | | - ); |
115 | | -} |
| 122 | +const accountUtils = new AccountUtils(); |
116 | 123 |
|
117 | | -AccountUtils.prototype.getLeaderboardAccounts = function(amount, done) { |
118 | | - dbUtils.performQuery( |
119 | | - "SELECT * FROM Users ORDER BY score DESC LIMIT 20", |
120 | | - [], |
121 | | - function (error, results, fields) { |
122 | | - if (error) { |
123 | | - done(dbUtils.convertSqlErrorToText(error), null); |
124 | | - return; |
125 | | - } |
126 | | - done(null, results); |
127 | | - } |
128 | | - ); |
129 | | -} |
| 124 | +module.exports = { accountUtils }; |
| 125 | + |
| 126 | +const { dbUtils } = require("./dbUtils"); |
130 | 127 |
|
131 | 128 |
|
0 commit comments