您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

verifyemail.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'email',
  5. docs: {
  6. get: {
  7. description: 'verifies confirmation email',
  8. notes: 'Verifies the email from the stored hash',
  9. },
  10. },
  11. }
  12. module.exports = {
  13. method: 'GET',
  14. path: '/verify/{hashedEmail}',
  15. options: {
  16. ...pluginConfig.docs.get,
  17. tags: ['api'],
  18. auth: false,
  19. cors: true,
  20. handler: async function (request, h) {
  21. const { userService } = request.server.services()
  22. const hash = request.params.hashedEmail
  23. try {
  24. const hashToMatch = Object.keys(userService.hashedEmails).find(
  25. email => {
  26. return email === hash
  27. },
  28. )
  29. const now = Date.now()
  30. // TODO: convert this back to a date object
  31. const expiration = userService.hashedEmails[hashToMatch]
  32. if (now > expiration) {
  33. delete userService.hashedEmails[hashToMatch]
  34. throw new Error(
  35. 'you took to long to respond to the email...',
  36. )
  37. }
  38. if (!hashToMatch) {
  39. throw new Error('no record of email in cache')
  40. }
  41. return {
  42. ok: true,
  43. handler: pluginConfig.handlerType,
  44. data: { hashesMatch: hashToMatch === hash },
  45. }
  46. } catch (err) {
  47. console.log('err :=>', err)
  48. return {
  49. ok: false,
  50. handler: pluginConfig.handlerType,
  51. data: {
  52. error: err,
  53. },
  54. }
  55. }
  56. },
  57. validate: {
  58. failAction: 'log',
  59. },
  60. response: {
  61. schema: Joi.object({
  62. ok: Joi.bool(),
  63. handler: Joi.string(),
  64. data: Joi.object(),
  65. }).label('verify_email_res'),
  66. failAction: 'log',
  67. },
  68. },
  69. }