reset-pwd-by-email.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 userMatched = await findUser.call(this, {
  77. userQuery: {
  78. email
  79. },
  80. authorizedApp: [this.getClientInfo().appId]
  81. })
  82. if (userMatched.length === 0) {
  83. throw {
  84. errCode: ERROR.ACCOUNT_NOT_EXISTS
  85. }
  86. } else if (userMatched.length > 1) {
  87. throw {
  88. errCode: ERROR.ACCOUNT_CONFLICT
  89. }
  90. }
  91. const { _id: uid } = userMatched[0]
  92. const {
  93. passwordHash,
  94. version
  95. } = new PasswordUtils({
  96. passwordSecret: this.config.passwordSecret
  97. }).generatePasswordHash({
  98. password
  99. })
  100. // 更新用户密码
  101. await userCollection.doc(uid).update({
  102. password: passwordHash,
  103. password_secret_version: version,
  104. valid_token_date: Date.now()
  105. })
  106. // 写入成功日志
  107. await this.middleware.uniIdLog({
  108. data: {
  109. email
  110. },
  111. type: LOG_TYPE.RESET_PWD_BY_SMS
  112. })
  113. return {
  114. errCode: 0
  115. }
  116. }