Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

user.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 { id } = await User.query(txn).insert(userInfo)
  55. await this.changePassword(id, password, txn)
  56. return id
  57. }
  58. /**
  59. * Updates user's info
  60. * @param {number} id
  61. * @param {*} param1
  62. * @param {*} txn
  63. * @returns
  64. */
  65. async update(id, { password, ...userInfo }, txn) {
  66. const { User } = this.server.models()
  67. if (Object.keys(userInfo).length > 0) {
  68. await User.query(txn)
  69. .throwIfNotFound()
  70. .where({ id })
  71. .patch(userInfo)
  72. }
  73. if (password) {
  74. await this.changePassword(id, password, txn)
  75. }
  76. return id
  77. }
  78. /**
  79. * Self explanatory
  80. * @param {*} param0
  81. * @param {*} txn
  82. * @returns
  83. */
  84. async login({ email, password }, txn) {
  85. const { User } = this.server.models()
  86. const user = await User.query(txn)
  87. .throwIfNotFound()
  88. .first()
  89. .where({ user_email: email })
  90. /** Uncomment to run password check using SecurePassword */
  91. // const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
  92. // if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
  93. // await this.changePassword(user.id, password, txn)
  94. // }
  95. // else if (passwordCheck !== SecurePassword.VALID) {
  96. // throw User.createNotFoundError()
  97. // }
  98. return user
  99. }
  100. /**
  101. * Create a token to be sent in request headers
  102. * @param {User} user
  103. * @returns {Token}
  104. */
  105. createToken(user) {
  106. const key = this.server.registrations['main-app-plugin'].options.jwtKey
  107. return Jwt.token.generate(
  108. {
  109. aud: 'urn:audience:test',
  110. iss: 'urn:issuer:test',
  111. email: user.user_email,
  112. },
  113. {
  114. key: key,
  115. algorithm: 'HS256',
  116. },
  117. {
  118. ttlSec: 4 * 60 * 60, // 7 days
  119. },
  120. )
  121. }
  122. /**
  123. * Use knex to try to change password entry
  124. * @param {number} id
  125. * @param {string} password
  126. * @param {*} txn
  127. * @returns {number}
  128. */
  129. async changePassword(id, password, txn) {
  130. const { User } = this.server.models()
  131. await User.query(txn)
  132. .throwIfNotFound()
  133. .where({ id })
  134. .patch({
  135. password: await this.pwd.hash(Buffer.from(password)),
  136. })
  137. return id
  138. }
  139. }