|
|
@@ -5,7 +5,12 @@ const Jwt = require('@hapi/jwt');
|
|
5
|
5
|
const Schmervice = require('@hapipal/schmervice');
|
|
6
|
6
|
const SecurePassword = require('secure-password');
|
|
7
|
7
|
|
|
|
8
|
+/** Class for methods used in the User plugin */
|
|
8
|
9
|
module.exports = class UserService extends Schmervice.Service {
|
|
|
10
|
+ /**
|
|
|
11
|
+ * Unsure of what our constructor does
|
|
|
12
|
+ * @param {...any} args
|
|
|
13
|
+ */
|
|
9
|
14
|
constructor(...args) {
|
|
10
|
15
|
super(...args)
|
|
11
|
16
|
const pwd = new SecurePassword()
|
|
|
@@ -14,21 +19,50 @@ module.exports = class UserService extends Schmervice.Service {
|
|
14
|
19
|
verify: Util.promisify(pwd.verify.bind(pwd))
|
|
15
|
20
|
}
|
|
16
|
21
|
}
|
|
|
22
|
+
|
|
|
23
|
+ /**
|
|
|
24
|
+ * Use knex to find users with id column
|
|
|
25
|
+ * @param {number} id
|
|
|
26
|
+ * @param {*} txn
|
|
|
27
|
+ * @returns
|
|
|
28
|
+ */
|
|
17
|
29
|
async findById(id, txn) {
|
|
18
|
30
|
const { User } = this.server.models()
|
|
19
|
31
|
return await User.query(txn).throwIfNotFound().findById(id)
|
|
20
|
32
|
}
|
|
|
33
|
+
|
|
|
34
|
+ /**
|
|
|
35
|
+ * Use knew to find first user with username
|
|
|
36
|
+ * @param {*} username
|
|
|
37
|
+ * @param {*} txn
|
|
|
38
|
+ * @returns
|
|
|
39
|
+ */
|
|
21
|
40
|
async findByUsername(username, txn) {
|
|
22
|
41
|
const { User } = this.server.models()
|
|
23
|
42
|
|
|
24
|
43
|
return await User.query(txn).throwIfNotFound().first().where({ username })
|
|
25
|
44
|
}
|
|
|
45
|
+
|
|
|
46
|
+ /**
|
|
|
47
|
+ * Signup function
|
|
|
48
|
+ * @param {*} param0
|
|
|
49
|
+ * @param {*} txn
|
|
|
50
|
+ * @returns
|
|
|
51
|
+ */
|
|
26
|
52
|
async signup({ password, ...userInfo }, txn) {
|
|
27
|
53
|
const { User } = this.server.models()
|
|
28
|
54
|
const { id } = await User.query(txn).insert(userInfo)
|
|
29
|
55
|
await this.changePassword(id, password, txn)
|
|
30
|
56
|
return id
|
|
31
|
57
|
}
|
|
|
58
|
+
|
|
|
59
|
+ /**
|
|
|
60
|
+ * Updates user's info
|
|
|
61
|
+ * @param {number} id
|
|
|
62
|
+ * @param {*} param1
|
|
|
63
|
+ * @param {*} txn
|
|
|
64
|
+ * @returns
|
|
|
65
|
+ */
|
|
32
|
66
|
async update(id, { password, ...userInfo }, txn) {
|
|
33
|
67
|
const { User } = this.server.models()
|
|
34
|
68
|
|
|
|
@@ -42,6 +76,13 @@ module.exports = class UserService extends Schmervice.Service {
|
|
42
|
76
|
|
|
43
|
77
|
return id
|
|
44
|
78
|
}
|
|
|
79
|
+
|
|
|
80
|
+ /**
|
|
|
81
|
+ * Self explanatory
|
|
|
82
|
+ * @param {*} param0
|
|
|
83
|
+ * @param {*} txn
|
|
|
84
|
+ * @returns
|
|
|
85
|
+ */
|
|
45
|
86
|
async login({ email, password }, txn) {
|
|
46
|
87
|
const { User } = this.server.models()
|
|
47
|
88
|
|
|
|
@@ -51,7 +92,7 @@ module.exports = class UserService extends Schmervice.Service {
|
|
51
|
92
|
.where({ user_email: email })
|
|
52
|
93
|
|
|
53
|
94
|
|
|
54
|
|
- // Uncomment to run password check using SecurePassword
|
|
|
95
|
+ /** Uncomment to run password check using SecurePassword */
|
|
55
|
96
|
// const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
|
|
56
|
97
|
// if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
|
|
57
|
98
|
// await this.changePassword(user.id, password, txn)
|
|
|
@@ -62,6 +103,12 @@ module.exports = class UserService extends Schmervice.Service {
|
|
62
|
103
|
|
|
63
|
104
|
return user
|
|
64
|
105
|
}
|
|
|
106
|
+
|
|
|
107
|
+ /**
|
|
|
108
|
+ * Create a token to be sent in request headers
|
|
|
109
|
+ * @param {User} user
|
|
|
110
|
+ * @returns {Token}
|
|
|
111
|
+ */
|
|
65
|
112
|
createToken(user) {
|
|
66
|
113
|
const key = this.server.registrations['main-app-plugin'].options.jwtKey
|
|
67
|
114
|
|
|
|
@@ -77,6 +124,14 @@ module.exports = class UserService extends Schmervice.Service {
|
|
77
|
124
|
ttlSec: 4 * 60 * 60 // 7 days
|
|
78
|
125
|
})
|
|
79
|
126
|
}
|
|
|
127
|
+
|
|
|
128
|
+ /**
|
|
|
129
|
+ * Use knex to try to change password entry
|
|
|
130
|
+ * @param {number} id
|
|
|
131
|
+ * @param {string} password
|
|
|
132
|
+ * @param {*} txn
|
|
|
133
|
+ * @returns {number}
|
|
|
134
|
+ */
|
|
80
|
135
|
async changePassword(id, password, txn) {
|
|
81
|
136
|
const { User } = this.server.models()
|
|
82
|
137
|
|