reset-pwd-by-email.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const {
  2. ERROR
  3. } = require('../../common/error')
  4. const {
  5. getNeedCaptcha,
  6. verifyCaptcha
  7. } = require('../../lib/utils/captcha')
  8. const {
  9. verifyEmailCode
  10. } = require('../../lib/utils/verify-code')
  11. const {
  12. userCollection,
  13. EMAIL_SCENE,
  14. CAPTCHA_SCENE,
  15. LOG_TYPE
  16. } = require('../../common/constants')
  17. const {
  18. findUser
  19. } = require('../../lib/utils/account')
  20. const PasswordUtils = require('../../lib/utils/password')
  21. /**
  22. * 通过邮箱验证码重置密码
  23. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#reset-pwd-by-email
  24. * @param {object} params
  25. * @param {string} params.email 邮箱
  26. * @param {string} params.code 邮箱验证码
  27. * @param {string} params.password 密码
  28. * @param {string} params.captcha 图形验证码
  29. * @returns {object}
  30. */
  31. module.exports = async function (params = {}) {
  32. const schema = {
  33. email: 'email',
  34. code: 'string',
  35. password: 'password',
  36. captcha: {
  37. required: false,
  38. type: 'string'
  39. }
  40. }
  41. this.middleware.validate(params, schema)
  42. const {
  43. email,
  44. code,
  45. password,
  46. captcha
  47. } = params
  48. const needCaptcha = await getNeedCaptcha.call(this, {
  49. email,
  50. type: LOG_TYPE.RESET_PWD_BY_EMAIL
  51. })
  52. if (needCaptcha) {
  53. await verifyCaptcha.call(this, {
  54. captcha,
  55. scene: CAPTCHA_SCENE.RESET_PWD_BY_EMAIL
  56. })
  57. }
  58. try {
  59. // 验证手机号验证码,验证不通过时写入失败日志
  60. await verifyEmailCode({
  61. email,
  62. code,
  63. scene: EMAIL_SCENE.RESET_PWD_BY_EMAIL
  64. })
  65. } catch (error) {
  66. await this.middleware.uniIdLog({
  67. data: {
  68. email
  69. },
  70. type: LOG_TYPE.RESET_PWD_BY_EMAIL,
  71. success: false
  72. })
  73. throw error
  74. }
  75. // 根据手机号查找匹配的用户
  76. const {
  77. total,
  78. userMatched
  79. } = await findUser.call(this, {
  80. userQuery: {
  81. email
  82. },
  83. authorizedApp: [this.getUniversalClientInfo().appId]
  84. })
  85. if (userMatched.length === 0) {
  86. if (total > 0) {
  87. throw {
  88. errCode: ERROR.ACCOUNT_NOT_EXISTS_IN_CURRENT_APP
  89. }
  90. }
  91. throw {
  92. errCode: ERROR.ACCOUNT_NOT_EXISTS
  93. }
  94. } else if (userMatched.length > 1) {
  95. throw {
  96. errCode: ERROR.ACCOUNT_CONFLICT
  97. }
  98. }
  99. const { _id: uid } = userMatched[0]
  100. const {
  101. passwordHash,
  102. version
  103. } = new PasswordUtils({
  104. clientInfo: this.getUniversalClientInfo(),
  105. passwordSecret: this.config.passwordSecret
  106. }).generatePasswordHash({
  107. password
  108. })
  109. // 更新用户密码
  110. await userCollection.doc(uid).update({
  111. password: passwordHash,
  112. password_secret_version: version,
  113. valid_token_date: Date.now()
  114. })
  115. // 写入成功日志
  116. await this.middleware.uniIdLog({
  117. data: {
  118. email
  119. },
  120. type: LOG_TYPE.RESET_PWD_BY_SMS
  121. })
  122. return {
  123. errCode: 0
  124. }
  125. }