Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

user.js 4.3KB

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