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

verify-session.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. console.log('hashToMatch :=>', hashToMatch)
  30. if (!hashToMatch?.length) {
  31. throw Error('hashToMatch Not Found!')
  32. }
  33. const now = Date.now()
  34. const expiration = new Date(
  35. userService.activeSessions[`${hash}`].expiration,
  36. )
  37. if (now > expiration) {
  38. delete userService.activeSessions[hashToMatch]
  39. throw new Error(
  40. 'you took to long to respond to the email...',
  41. )
  42. }
  43. if (!hashToMatch) {
  44. throw new Error('no record of email in cache')
  45. }
  46. // NOTE: When user responds to email,
  47. // boolean value is set to true, allowing user back into the survey
  48. userService.activeSessions[
  49. hashToMatch
  50. ].emailWasRespondedTo = true
  51. return {
  52. ok: true,
  53. handler: pluginConfig.handlerType,
  54. data: {
  55. hashesMatch: hashToMatch === hash,
  56. },
  57. }
  58. } catch (err) {
  59. console.log('err :=>', err)
  60. return {
  61. ok: false,
  62. handler: pluginConfig.handlerType,
  63. data: {
  64. error: err,
  65. },
  66. }
  67. }
  68. },
  69. validate: {
  70. failAction: 'log',
  71. },
  72. response: {
  73. schema: Joi.object({
  74. ok: Joi.bool(),
  75. handler: Joi.string(),
  76. data: Joi.object(),
  77. }).label('verify_email_res'),
  78. failAction: 'log',
  79. },
  80. },
  81. }