選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

user.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict'
  2. const Util = require('util')
  3. const Jwt = require('@hapi/jwt')
  4. const Schmervice = require('@hapipal/schmervice')
  5. const SecurePassword = require('secure-password')
  6. /** Class for methods used in the User plugin */
  7. module.exports = class UserService extends Schmervice.Service {
  8. /**
  9. * Unsure of what our constructor does
  10. * @param {...any} args
  11. */
  12. constructor(...args) {
  13. super(...args)
  14. const pwd = new SecurePassword()
  15. this.pwd = {
  16. hash: Util.promisify(pwd.hash.bind(pwd)),
  17. verify: Util.promisify(pwd.verify.bind(pwd)),
  18. }
  19. }
  20. /**
  21. * Use knex to find users with id column
  22. * @param {number} id
  23. * @param {*} txn
  24. * @returns
  25. */
  26. async findById(id, txn) {
  27. const { User } = this.server.models()
  28. return await User.query(txn)
  29. .throwIfNotFound()
  30. .first()
  31. .where({ user_id: id })
  32. }
  33. /**
  34. * Use knew to find first user with username
  35. * @param {*} username
  36. * @param {*} txn
  37. * @returns
  38. */
  39. async findByUsername(username, txn) {
  40. const { User } = this.server.models()
  41. return await User.query(txn)
  42. .throwIfNotFound()
  43. .first()
  44. .where({ user_name: username })
  45. }
  46. /**
  47. * Signup function
  48. * @param {*} param0
  49. * @param {*} txn
  50. * @returns
  51. */
  52. async signup({ password, userInfo }, txn) {
  53. const { User } = this.server.models()
  54. const matchingEmails = await User.query().where(
  55. 'user_email',
  56. userInfo.user_email,
  57. )
  58. if (matchingEmails.length > 0) {
  59. throw `User ${userInfo.user_email} already exists: Cannot create a user without a unique email`
  60. }
  61. const user = await User.query(txn).insert(userInfo)
  62. user.user_id = user.id
  63. delete user.id
  64. // await this.changePassword(id, password, txn)
  65. return user
  66. }
  67. /**
  68. * Updates user's info
  69. * @param {number} id
  70. * @param {*} param1
  71. * @param {*} txn
  72. * @returns
  73. */
  74. async update(id, { password, ...userInfo }, txn) {
  75. const { User } = this.server.models()
  76. if (Object.keys(userInfo).length > 0) {
  77. await User.query(txn)
  78. .throwIfNotFound()
  79. .where({ id })
  80. .patch(userInfo)
  81. }
  82. if (password) {
  83. await this.changePassword(id, password, txn)
  84. }
  85. return id
  86. }
  87. /**
  88. * Self explanatory
  89. * @param {*} param0
  90. * @param {*} txn
  91. * @returns
  92. */
  93. async login({ email, password }, txn) {
  94. const { User } = this.server.models()
  95. const user = await User.query(txn)
  96. .throwIfNotFound()
  97. .first()
  98. .where({ user_email: email })
  99. /** Uncomment to run password check using SecurePassword */
  100. // const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
  101. // if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
  102. // await this.changePassword(user.id, password, txn)
  103. // }
  104. // else if (passwordCheck !== SecurePassword.VALID) {
  105. // throw User.createNotFoundError()
  106. // }
  107. return user
  108. }
  109. /**
  110. * Create a token to be sent in request headers
  111. * @param {User} user
  112. * @returns {Token}
  113. */
  114. createToken(user) {
  115. const key = this.server.registrations['main-app-plugin'].options.jwtKey
  116. return Jwt.token.generate(
  117. {
  118. aud: 'urn:audience:test',
  119. iss: 'urn:issuer:test',
  120. email: user.user_email,
  121. },
  122. {
  123. key: key,
  124. algorithm: 'HS256',
  125. },
  126. {
  127. ttlSec: 4 * 60 * 60, // 7 days
  128. },
  129. )
  130. }
  131. /**
  132. * Use knex to try to change password entry
  133. * @param {number} id
  134. * @param {string} password
  135. * @param {*} txn
  136. * @returns {number}
  137. */
  138. async changePassword(id, password, txn) {
  139. const { User } = this.server.models()
  140. return 'done'
  141. // await User.query(txn)
  142. // .throwIfNotFound()
  143. // .where({ id })
  144. // .patch({
  145. // password: await this.pwd.hash(Buffer.from(password)),
  146. // })
  147. // return id
  148. }
  149. }