Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

verifyemail.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/{hashedSessionToken}',
  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.hashedSessionToken
  23. try {
  24. const hashToMatch = Object.keys(
  25. userService.activeSessions,
  26. ).find(hashedToken => {
  27. return hashedToken === hash
  28. })
  29. const now = Date.now()
  30. // TODO: convert this back to a date object
  31. const expiration =
  32. userService.activeSessions[`${hash}`].expiration
  33. if (now > expiration) {
  34. delete userService.activeSessions[hashToMatch]
  35. throw new Error(
  36. 'you took to long to respond to the email...',
  37. )
  38. }
  39. if (!hashToMatch) {
  40. throw new Error('no record of email in cache')
  41. }
  42. return {
  43. ok: true,
  44. handler: pluginConfig.handlerType,
  45. data: {
  46. hashesMatch: hashToMatch === hash,
  47. sessionToken:
  48. userService.activeSessions[`${hash}`].sessionToken,
  49. },
  50. }
  51. } catch (err) {
  52. console.log('err :=>', err)
  53. return {
  54. ok: false,
  55. handler: pluginConfig.handlerType,
  56. data: {
  57. error: err,
  58. },
  59. }
  60. }
  61. },
  62. validate: {
  63. failAction: 'log',
  64. },
  65. response: {
  66. schema: Joi.object({
  67. ok: Joi.bool(),
  68. handler: Joi.string(),
  69. data: Joi.object(),
  70. }).label('verify_email_res'),
  71. failAction: 'log',
  72. },
  73. },
  74. }